Ambiera ForumDiscussions, Help and Support. |
|
|
|||||
|
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:
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:
|
||||
|
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: /* As for the "Init" function or something, there's no such thing besides that "behavior_test = function()" thing. |
||||
|
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: /* 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:
I can now refer to 'self' as 'this' from within any function now :) Weird why print(this) always returns 0,0,0 ?? |
||||
|
Try this (hahahahahahahaha):
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. |
||||
|
this wrote: Try this (hahahahahahahaha):
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:
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:
|
||||
|
@gazzauk The example code I provided was actually part of a behavior script.
|
||||
|
happy debugging. |
||||
|
"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. |
||||
|
I don't think you understand the issue here...it's the behaviour properties that I'm referring to here:
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. |
||||
|
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...
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. |
||||
|
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. |
||||
|
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. |
||||
|
If you don't want to use a flag to check if properties are initialized, it is also possible to redefine onAnimate method /* |
||||
|
okeoke brilliant idea! that's such a simple and quick solution. |
||||
|
|