CloneWithBehavior() does not copy Class
by James Ford · in Torque Game Builder · 08/29/2007 (6:53 pm) · 2 replies
The scripting class field does not seem to be copied when an object in cloned, this is a big problem if you have script class methods / callbacks like an oncollision. The result is that my cloned objects appear to have no oncollision and this took me a while to figure out why. Also, this didn't work:
%clone = %template.clonewithbehaviors();
%clone.class = %template.class;
Although the class field is now the same as the templates class field something internally must not be hooked up properly because the oncollision callback for that class is still not called when the clone collides. Is this the wrong way to change an objects class after it has been created? I didn't see a method like SimObject.SetClass(). Or is there no current way to change a class after an object is created (note im talking about script classes not tgb classes) that will be reflected in oncollision callbacks.
%clone = %template.clonewithbehaviors();
%clone.class = %template.class;
Although the class field is now the same as the templates class field something internally must not be hooked up properly because the oncollision callback for that class is still not called when the clone collides. Is this the wrong way to change an objects class after it has been created? I didn't see a method like SimObject.SetClass(). Or is there no current way to change a class after an object is created (note im talking about script classes not tgb classes) that will be reflected in oncollision callbacks.
About the author
http://jamesdev.info
Associate James Ford
Sickhead Games
I was using clonewithbehaviors, which does not copy the class (or any dynmic fields).
The normal clone method can be passed a boolian parameter which specifies whether to copy dynamic fields, and tests show that using clone() does not copy the class and clone(true) does. So, I believe clonewithbehaviors needs a boolian parameter and to behave like the original clone method.
In the mean time I have worked around the problem by defining my oncollision callbacks within a behavior namespace instead of a class namespace. But I believe you should be able to mix and match, that is, define some "behavior" in behaviors and some by class.