Game Development Community

Methods don't exist after cloneWithBehaviors()?

by Rick Smorawski · in Torque Game Builder · 09/25/2007 (11:01 am) · 3 replies

I apologize if I am missing a basic concept when it comes to Torque Scripting, but I am a bit confused by some behavior.

I have two 2dStaticSprite's 'ship' and 'particle1'

ship has the packaged 'Shoots' behavior, with it's projectile defined as 'particle1'

My goal is to allow the 'particle1' to bounce three times before it is removed from the scene. I attempted to create a class 'projectile' and create a projectile::onCollision() to keep track of the life span.

However, in the following case (from inside shoots.cs)

%projectile = %this.projectile.cloneWithBehaviors();
warn("Checking : " @ %projectile.isMethod("onCollision"));

returns false.

%projectile.class does return 'projectile' so it's class is being properly defined.

All the proper files are indeed being exec()'d

As you can probably tell I'm rather new to TGB Scripting, so I may be overlooking something obvious.

Thanks for the help!
Rick

About the author

Recent Threads


#1
09/26/2007 (12:19 pm)
I believe this is a symptom of the same "bug" I found here.
If you are using behaviors and clonewithbehaviors for creating new items, then i suggest defining your oncollision function as a member of some behavior rather than a class name, then it will be sure to be copied.
www.garagegames.com/mg/forums/result.thread.php?qt=66668
#2
10/05/2007 (4:05 pm)
Hi Rick, if you need to use class methods with behaviours have a look into using clone instead of cloneWithBehaviors. I had a similar issue recently. I wanted to use the mouse to fire off a projectile that exploded at a target (think missile command). So, I created a MouseShootsAt behaviour to handle the shooting itself. I needed class functionality not behavioural functionality for the projectile therefore I used clone in the behaviour to create the projectiles like this...

// create the projectile
   %projectile = %this.projectile.clone(true);
   %projectile.setPosition(%this.owner.position);
   %projectile.setRotation(%this.owner.rotation);
   %projectile.moveTo(%this.target.getPosition(), %this.projectileSpeed, true, true, false,  0.01);

This enabled me to use the onPositionTarget() callback to call an explode function on my projectile and everything worked fine.

Clone does not copy behaviours so if the projectile itself needs to have behaviours then you should add them dynamically. There are a few posts on this board about how to do that.

I totally agree that cloneWithBehaviors should really work like clone and have the option to handle classes correctly. It would save a few headaches and promote developer happiness :)
#3
10/06/2007 (3:15 pm)
Thank you both very much for your help! I haven't had a chance to get back to this project recently, but I'll try the work arounds you suggested. Thanks !

Rick