ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Help with CopperCube
forum topic indicator behavior
person icon
tapocalypse
Registered User
Quote
2025-08-06 12:54:21

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?

person icon
Monks
Guest
Quote
2025-08-06 14:47:29

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?

person icon
tapocalypse
Registered User
Quote
2025-08-06 17:01:07

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.

person icon
guest_guest
Guest
Quote
2025-08-06 20:31:46

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.

person icon
okeoke
Registered User
Quote
2025-08-06 21:18:37

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
embedded external image
🔎︎
. You can access it from your behaviors:
embedded external image
🔎︎
embedded external image
🔎︎


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 () {
this.lastTime = null;
this.propInt = 2;
this.propString = 'this is a string';
this.changeSceneNextFrame = false;
};

behavior_TestVars.prototype.onAnimate = function (node, timeMs) {
// some logic here that changes this.changeSceneNextFrame to true
// ...

if (this.changeSceneNextFrame) {
this.changeSceneNextFrame = false;
// reset properties
this.propInt = 2;
this.propString = 'this is a string';
ccbSwitchToScene('scene_2');
}
}
You can also store default values as props if required. Like:
    this.propStringDefault = 'this is a string';
this.propString = this.propStringDefault;

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;

var behavior_TestVars = function () {
globalVar = this;
this.a = 123;
};

behavior_TestVars.prototype.onAnimate = function (node, timeMs) {
// reference instance from anywhere using global var
print(globalVar.a);
}
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 = [];

var behavior_TestVars = function () {
globalVar.push(this);
this.a = 123;
};

behavior_TestVars.prototype.onAnimate = function (node, timeMs) {
// reference instance from anywhere using global var
// here it's instance with index 0 but you can also iterate through the array
print(globalVar[0].a);
}



Create reply:










 

  

Possible Codes


Feature Code
Link [url] www.example.com [/url]
Bold [b]bold text[/b]
Image [img]http://www.example.com/image.jpg[/img]
Quote [quote]quoted text[/quote]
Code [code]source code[/code]

Emoticons


icon_holyicon_cryicon_devilicon_lookicon_grinicon_kissicon_monkeyicon_hmpf
icon_sadicon_happyicon_smileicon_uhicon_blink   






Copyright© Ambiera e.U. all rights reserved.
Contact | Imprint | Products | Privacy Policy | Terms and Conditions |