Creating a persistant object to clone from
by Daniel Stoneley · in Torque Game Builder · 05/12/2008 (1:54 pm) · 7 replies
Hi there poeple.
I'm just about to start some rapid prototyping sessions over at my website (http://www.shmup-dev.com/forum/) so I figured now would be a good time to start learning TGB since I bought it ages ago but havn't played with it yet. Therefore you might be seeing a lot of me in the near future ;p.
Anyway I'm having a beginners problem to do with object cloning and I was hopeing someone could point me in the right direction. Say I create a gun object in the editor and assign it a behavior like the 'Timer_Shoots' behavior which instructs it to repeatedly clone a projectile object and shoot it based on a timer. In the editor still I then create a projectile object and name it so that I can assign it as the clone object for the gun. This is all fine and dandy the gun merrily shoots projectiles.
Now however say I wish to give the projectiles a finite lifetime. Therefore in my projectile object in the editor I set lifetime to 2 seconds say. Now what happens is that after 2 seconds have passed the projectile dissapears as you would expect. However the gun also now stops firing projectiles since the object it was cloning has disappeared.
What I really need is to be able to create a reference object for cloning which is persistant but still passes on behaviors such as lifetime and world limits without being effected by them itself. Any ideas how I can get around this problem?
I'm just about to start some rapid prototyping sessions over at my website (http://www.shmup-dev.com/forum/) so I figured now would be a good time to start learning TGB since I bought it ages ago but havn't played with it yet. Therefore you might be seeing a lot of me in the near future ;p.
Anyway I'm having a beginners problem to do with object cloning and I was hopeing someone could point me in the right direction. Say I create a gun object in the editor and assign it a behavior like the 'Timer_Shoots' behavior which instructs it to repeatedly clone a projectile object and shoot it based on a timer. In the editor still I then create a projectile object and name it so that I can assign it as the clone object for the gun. This is all fine and dandy the gun merrily shoots projectiles.
Now however say I wish to give the projectiles a finite lifetime. Therefore in my projectile object in the editor I set lifetime to 2 seconds say. Now what happens is that after 2 seconds have passed the projectile dissapears as you would expect. However the gun also now stops firing projectiles since the object it was cloning has disappeared.
What I really need is to be able to create a reference object for cloning which is persistant but still passes on behaviors such as lifetime and world limits without being effected by them itself. Any ideas how I can get around this problem?
#2
Edit: Ah ok, I understand now. Template behavior is an already existing behavior which disables on level load. That makes sense. Thanks very much for the help, it's going to take me a while to get my head around this :).
05/12/2008 (2:34 pm)
Well that's kind of the problem. I might want to give my projectiles behaviors which destroy them after x seconds or which influence their movement and hence tend to move them outside of the world limits. Now however if I give the reference projectile such a behavior so that those behaviors are cloned, they also influence the reference projectile. Therefore once the reference projectile is innevitably destroyed, no more cloning takes place. Is this making sense?Edit: Ah ok, I understand now. Template behavior is an already existing behavior which disables on level load. That makes sense. Thanks very much for the help, it's going to take me a while to get my head around this :).
#3
05/12/2008 (2:54 pm)
No problem. Think of it this way - when you give the template behavior (plus whatever other behaviors) to a "master" object, this object is then disabled when the level is loaded. Since it is disabled it won't be destroyed after X seconds, etc. When creating a clone of this "master" object with cloneWithBehaviors(), since the level is already loaded, the template behavior that all the clones inherited does nothing and the clones remain enabled in the scene and do what you want them to.
#4
05/15/2008 (12:21 am)
Good to see more a fellow shmupper using TGB! (I'm monoRAIL on shmup-dev).
#5
05/20/2008 (2:06 am)
How do. Thought I might bump into you around here at some point given that drone swarm was built using TGB. I'm motorherp btw if you hadn't figured that out already. Looks like I might be prodding you for advice on TGB over at the shmup-dev forums from now on given I'm using this for the rapid prototype sessions :).
#6
05/20/2008 (9:15 am)
I modified the template behavior so it calls onBehaviorRemove for all other behaviors on the owner. This allowed me to "really" disable all other behaviors on the template object. I can add code to the onBehaviorRemove callback of any behavior with needs some code to disable it ( cancel schedules, unbind keys, etc.. )
#7
now, id like to say somethin about the subject.
when i use turrets in a game anf want to limit its life time, basically what i do is just create a new instance of the bullet object, and do 2 things:
1) set its world limits so when it hits it, it dies (the bullet, i mean)
2) if i want it to dissapear at a given time, schedule a safeDelete for that given time...
now, the second point cant be abused, as you already know, since it will hog the engine and give you some headaches... but this way, you avoid havin extra objects in the scene (you just create every instace with everything you need... basically it works a lil like cloning).
anyway... im a newb compared to you guys, but that method worked for me.
05/20/2008 (11:23 am)
Hello there, motorherp and monoRAIL... its pretty good to see you around here... im Kaspal in shmup-dev, btw.now, id like to say somethin about the subject.
when i use turrets in a game anf want to limit its life time, basically what i do is just create a new instance of the bullet object, and do 2 things:
1) set its world limits so when it hits it, it dies (the bullet, i mean)
2) if i want it to dissapear at a given time, schedule a safeDelete for that given time...
now, the second point cant be abused, as you already know, since it will hog the engine and give you some headaches... but this way, you avoid havin extra objects in the scene (you just create every instace with everything you need... basically it works a lil like cloning).
anyway... im a newb compared to you guys, but that method worked for me.
Torque Owner Mike Lilligreen
T2DTutorials