Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
1. How to reset variables initialized within scripted behavior's constructor? E.g. when leaving the scene that uses such behavior 2. Is there any way to access variables created using "Execute Javascript" behavior, from a scripted one? |
||||
|
If I understand your first question well,upon switching the scene you can set the variable to different value in the action window where change scene action is present etc. in order to reset it. For number 2, I'm not so sure but it's possible to access those variables from an extension if you add certain lines of code to it which I'm not so conversant with Do you mind telling the scenarios in which you want to get them to work? |
||||
|
The scenario I were planning to do is following: when I'm switching to another scene, some variables initialized in scripted behavior's constructor (behavior that is attached to the camera on current scene) should be set to their values from the beginning. Tried many ways to release this logic, such as: setting values with checking the "scene is active" flag variable within the scripted behavior beforehand - no proper result. |
||||
|
Alright, here’s the deal. To ensure your variables stays intact between scenes, store the variable in your first scene using "Load or Store a variable from or to disk" just before switching to the next one. You should also declare those variables as global ones, then in the second scene, use the "Load or Store a variable from or to disk" action to retrieve those variables. Attach this action to a "Before First Drawing Do Something" behavior in the second scene (in scene's behavior). This method guarantees variables transfer across scenes. |
||||
|
First of all, in order to understand how variables I strongly recommend you to learn about variables scope and function context if you haven't touched that topic before. Let's start with 2nd question: 1. All variables that you declare inside "Execute js" action are top level variables and they are accessible directly from any part of you application. You can access it from your behaviors: 2. If you want to reset object properties you can do it two ways: * Reset the whole scene using that corresponding action * Reset properties from the code If you call "change scene" command directly from behavior it is pretty straight forward: var behavior_TestVars = function () { You can also store default values as props if required. Like:this.propStringDefault = 'this is a string'; Basically you never modify propString but never touch the default value. Also remember that if you prop is not primitive value, i.e. array or object js stores "link" to object, not object itself. You should use Object.assign method or slice for array in order to duplicate it. In case you're calling "switch scene" from somewhere else you can also reference instance of your behavior class through global variable, like: var globalVar;In case there multiple instances of the same behavior, i.e. it is attached to multiple nodes you can collect all of those into array. Just remember that you also need to manage elements inside array, so you don't have duplicates, or items that do not exist anymore. In general looks like: var globalVar = []; |
||||
|
|