ambiera logo

Ambiera Forum

Discussions, Help and Support.

folder icon Ambiera Forum > CopperCube > Programming and Scripting
forum topic indicator Behavior Constructor or Init function
person icon
gazzauk
Registered User
Quote
2026-09-13 03:59:02

I'm a little confused with how behavior scripts do not have an init function where the properties of the behaviour can be read or checked for validity. for example:


/*
<behavior jsname="behavior_test" description="testing behavior">
<property name="SceneNode" type="scenenode" />
<property name="Speed" type="float" default="1.0" />
</behavior>
*/

var behavior_test = function()
{
// how do I access the behavior properties from the constructor ??
// what is 'this' referring to in the constructor ??
this.speedfactor = this.Speed * 0.1;
ccbSetSceneNodePositionWithoutCollision(this.SceneNode, 0,0,0);
};


How do I acces the properties from the constructor using 'this'.
I'm always getting 0 whenever I attempt to do this.

Also why is the function prototype Init not working:


// is this implemented within the engine ??
behavior_test.prototype.onInit = function(node)
{
print("init: " + node);
};


person icon
dekon_17
Registered User
Quote
2026-09-13 09:33:07

Tried to do so, but it seems like you can't access those within the... "initializer" or whatever. Tried it with the built-in 2D Jump-n-Run controller behavior, same result - can't access them.

You could try to use global variables, but it seems like a complete overkill for this problem. A easier solution (although not really optimal, in my utterly unprofessional opinion) would probably be something like this:
/*
<behavior jsname="behavior_test" description="testing behavior">
<property name="SceneNode" type="scenenode" />
<property name="Speed" type="float" default="1.0" />
</behavior>
*/

behavior_test = function()
{
this.init = true;
}

behavior_test.prototype.onAnimate = function ()
{
if (this.init)
{
delete this.init;
this.speedfactor = this.Speed * 0.1;
ccbSetSceneNodePositionWithoutCollision(this.SceneNode, 0,0,0);
print ('YAY!!!!!!!!!');
}
...

As for the "Init" function or something, there's no such thing besides that "behavior_test = function()" thing.

person icon
gazzauk
Registered User
Quote
2026-09-13 20:16:37

dekon_17 wrote:

You could try to use global variables, but it seems like a complete overkill for this problem. A easier solution (although not really optimal, in my utterly unprofessional opinion) would probably be something like this:
/*
<behavior jsname="behavior_test" description="testing behavior">
<property name="SceneNode" type="scenenode" />
<property name="Speed" type="float" default="1.0" />
</behavior>
*/

behavior_test = function()
{
this.init = true;
}

behavior_test.prototype.onAnimate = function ()
{
if (this.init)
{
delete this.init;
this.speedfactor = this.Speed * 0.1;
ccbSetSceneNodePositionWithoutCollision(this.SceneNode, 0,0,0);
print ('YAY!!!!!!!!!');
}
...



Thanks for your input dekon_17. So you are correct that this is not possible without using the trickery code that you suggested.

I must admit that I have never had to access the property values within the constructor before, but I was working on a script that required that and I spent a few wasted hours trying to work out what was going on.

Your approach of using the init variable seems the best option as even global vars cannot acces the behavior properties. It's not ideal from a performance view as the OnAnimate functions is a very tight running loop so less code checks is optimal. I guess it would not really be noticed for a simple if compare statement.

Well, your utterly unprofessional opinion works for now! Although not sure if it would be more ideal using the onFrameEvent with ccbRegisterOnFrameEvent ??

I'm currently using this trick for now:


var self = null;

function onFrameDrawing()
{
if (self == null) {
return;
}
}

function onInit(node)
{
if (self == null) { return; }
print("onInit: " + node);
print("onInit: " + self.node);
}

behavior_test = function() {
ccbRegisterOnFrameEvent(onFrameDrawing);
}

behavior_test.prototype.onAnimate = function(node, timeMs)
{
if (self == null) {
print("NO WAY BABY!!");
this.node = node;
self = this;
onInit(node);
return;
}
}



I can now refer to 'self' as 'this' from within any function now :)
Weird why print(this) always returns 0,0,0 ??

person icon
this
Guest
Quote
2026-09-15 05:16:07

Try this (hahahahahahahaha):

this.x = 111;
this.y = 222;
this.z = 333;
print(this);

This will output 111.0, 222.0, 333.0
Looks to me like the print() function treats some inputs as vector3d objects.
If this thing finds x,y,z attributes on an object it appears to blindly dump the contained values to the console.
But I may be wrong.

Note that print() should work as expected in the WebGL build.

This "issue" appears to be related to a post made by user bracer in 2019.
The title of the post was: "print ( new Date() ); returns 0,0,0 !!! Screenshots included."

Amazing.

person icon
gazzauk
Registered User
Quote
2026-09-16 15:25:00

this wrote:
Try this (hahahahahahahaha):

this.x = 111;
this.y = 222;
this.z = 333;
print(this);

Amazing.


Yes, that works for standard code within the editor. I'm talking here about behaviour scripts and the specifically the constructor function and the properties of the script that are passed in from the editor.

Interesting point though that you made about 'this' using the first 3 assignments as a vector3:

this.x = 1
this.y = 2
this.z = 3
this.a = 4
print(this)
print(this.a)


var object = new vector3d()
object.x = 1
object.y = 2
object.z = 3
object.a = 4
print(object)
print(object.a)


Appears that the 'object' can be assigned more properties than a vector3. Not quite sure if this would cause any memory or data loss issues when passing this object into functions and used this way ??

Seems also that this can be applied to all object type variables including functions:

var func = new function() { print("hello") }
func.x = 1
func.y = 2
func.z = 3
print(func)
func


person icon
this
Guest
Quote
2026-09-16 20:18:02

@gazzauk
The example code I provided was actually part of a behavior script.


// The following embedded xml is for the editor and describes how the behavior can be edited:
// Supported types are: int, float, string, bool, color, vect3d, scenenode, texture, action
/*
<behavior jsname="behavior_test" description="test">
</behavior>
*/

behavior_test = function()
{
this.x = 111;
this.y = 222;
this.z = 333;
};

behavior_test.prototype.onAnimate = function(node, timeMs)
{
print(this); //displays 111, 222, 333 in the Windows build, note that "this" is not a vector3d object

this.onAnimate = function() {};

return true;
}


person icon
this
Guest
Quote
2026-09-16 21:29:25

emoji icon_grinv this vemoji icon_grin


/*
<behavior jsname="behavior_test" description="test"></behavior>
*/

behavior_test = function()
{
this.x = 111;
this.z = 333;
print(this);

//Showcases a quirk in CopperCube's print() implementation and displays: 111, 0, 333
//The WebGL build displays: Object { x: 111, z: 333, onAnimate: onAnimate() }

//Bonus (SpiderMonkey-lib specific stuff exposed by CopperCube exe)

//The following code will (hopefully) be skipped on the WebGL build
if (this.__parent__ !== undefined) {
print(this.__count__); //number of attributes of "this" (in this case, it should display "2")
print(this.__parent__); //0.0, 0.0, 0.0
print(String(this.__parent__)); // "object global"
print(this.__parent__.toSource()); //internal js source-code present in the global scope

this.watch("x", function (id, old_value, new_value) {
print("change detected: " + String(id) + " was changed from " + String(old_value) + " to " + String(new_value));
return new_value; // return new value
});
this.x = 0;
}

};

behavior_test.prototype.onAnimate = function(node, timeMs)
{
this.onAnimate = function() {};
return true;
}


happy debugging.

person icon
this
Guest
Quote
2026-09-16 22:19:48

"this" jokes aside.

I was specifically replying to your question about why print(this) outputs 0,0,0.

"Weird why print(this) always returns 0,0,0 ??"

Sorry for derailing your thread.

person icon
gazzauk
Registered User
Quote
2026-09-17 00:34:43

I don't think you understand the issue here...it's the behaviour properties that I'm referring to here:


/*
<behavior jsname="behavior_test" description="testing behavior">
<property name="SceneNode" type="scenenode" />
<property name="Speed" type="float" default="1.0" />
</behavior>
*/


These properties are only accesible from within the onAnimate function using the 'this' keyword. Sure you can use 'this' as normal but not for the reasons I'm talking about.

person icon
this
Guest
Quote
2026-09-17 01:49:45

Again: The above posts were not replies to your initial question, but to your specific isolated question about print(this).

And to reiterate the answer given by dekon_17 in regard to your initial question:
The properties defined in your XML header (<property name="Speed" ... />) appear to be injected
into "this" AFTER the constructor function finishes executing.

There is no mystery.

But...


/*
<behavior jsname="behavior_test" description="test">
<property name="Speed" type="float" default="100.0" />
</behavior>
*/

behavior_test = function()
{
var me = this;

print("Executed A!");

// Use SpiderMonkey's native __defineSetter__ (does not work in the WebGL build!)
this.__defineSetter__('Speed', function(incomingValue) {
// Remove the setter
delete me.Speed;

// Assign the value
me.Speed = incomingValue;

print("SUCCESS! Value captured inside constructor: " + me.Speed);
});
};

behavior_test.prototype.onAnimate = function(node, timeMs)
{
print("Executed B!");
this.onAnimate = function() {};
return true;
}


Did a brief test, seems to work on the Windows build.
There might be a similar workaround for the WebGL build using different tools (haven't tested it).

To be honest: dekon_17 already demonstrated a more efficient and elegant way to achieve a similar outcome.

Sorry for derailing your thread.

person icon
gazzauk
Registered User
Quote
2026-09-17 02:46:28

this Ok, my bad. The __defineSetter__ is something I did not know about but looks really hacky. I don't think I will go that extreme though!

Thanks for pointing that out and you did not derail my thread.

person icon
this
Guest
Quote
2026-09-17 03:47:52

One small update: the __defineSetter__ method seems to work in the WebGL build. Tested in the latest version of Firefox.

However, the MDN Web Docs by Mozilla mark it as "deprecated", starting with a header that states: "Avoid using this feature in new projects".

Well. emoji icon_sad

person icon
okeoke
Registered User
Quote
2026-09-17 22:05:45

If you don't want to use a flag to check if properties are initialized, it is also possible to redefine onAnimate method
/*
<behavior jsname="behavior_test" description="testing behavior">
<property name="SceneNode" type="scenenode" />
<property name="Speed" type="float" default="1.0" />
</behavior>
*/

var behavior_test = function () {
};

behavior_test.prototype.onAnimate = function () {
// only run once on first frame
this.speedFactor = this.Speed * 0.1;
ccbSetSceneNodePositionWithoutCollision(this.SceneNode, 0, 0, 0);
// rewrite onAnimate
this.onAnimate = this.main;
}

behavior_test.prototype.main = function() {
// your behavior code
}


person icon
gazzauk
Registered User
Quote
2026-09-18 05:14:46

okeoke brilliant idea! that's such a simple and quick solution.


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 |