spawning objects
by Justin Mosiman · in Torque Game Builder · 01/22/2009 (8:16 pm) · 4 replies
Hello,
I've used TGE/TGEA off and on for the past couple of years and now I decided to start with something simple and create a 2D game. But I've got a question about spawning objects, how do you do it in game? I'm not used to the whole behaviors idea, which I think has something to do with it.
I have created my t2dSceneObjectDatablock, now I just want to spawn it on the screen. How can this be done? If possible, I'd like it to be done with behaviors so I can start to get the hang of them.
Thank you,
Justin
I've used TGE/TGEA off and on for the past couple of years and now I decided to start with something simple and create a 2D game. But I've got a question about spawning objects, how do you do it in game? I'm not used to the whole behaviors idea, which I think has something to do with it.
I have created my t2dSceneObjectDatablock, now I just want to spawn it on the screen. How can this be done? If possible, I'd like it to be done with behaviors so I can start to get the hang of them.
Thank you,
Justin
#2
I have created an AIControllerBehavior that gets added to the base of the AI. This behavior gets called correctly, and in the onAddToScene(%this, %scenegraph) I am trying to spawn the unit.
I've bought the platformer starter kit, so I've been basing my workings off of that, and I tried spawning a new object with:
The SpawnPointBehavior is directly ripped from the platformer starter kit, I didn't make any changes. In my datablocks.cs file, I have:
But it doesn't look like it is even getting that far. When I run the game with this, I get the warning:
The unit spawns with the idle animation, but I can't control that unit at all because DragonClass::onAddToScene never gets called. SpawnPointBehavior is definitely there, there is even a .dso created for it when the game executes. I can also select it in the GUI under the behaviors tab.
Am I totally misdirected? Or am I close?
01/23/2009 (2:21 pm)
Thanks for your help. When I posted that I didn't know much about behaviors, now I have a better idea.. except I can't get my object to spawn using behaviors. Here's what I've done:I have created an AIControllerBehavior that gets added to the base of the AI. This behavior gets called correctly, and in the onAddToScene(%this, %scenegraph) I am trying to spawn the unit.
I've bought the platformer starter kit, so I've been basing my workings off of that, and I tried spawning a new object with:
%object = new t2dAnimatedSprite() {
animationName = "DragonIdleAnimation";
canSaveDynamicFields = "1";
Position = "0.000 22.500";
size = "16.000 16.000";
_behavior0 = "SpawnPointBehavior TargetObject AIDragonTemplate";
};
%scenegraph.addToScene(%object);The SpawnPointBehavior is directly ripped from the platformer starter kit, I didn't make any changes. In my datablocks.cs file, I have:
datablock t2dSceneObjectDatablock(AIDragonTemplate)
{
Class = "DragonClass";
Size = "16.000 16.000";
Layer = "1";
CollisionPolyList = "-0.300 -0.250 0.300 -0.250 0.300 0.250 0.000 0.480 -0.300 0.250";
_Behavior0 = "UnitBehavior";
_Behavior1 = "UnitAnimationBehavior DatablockPrefix Dragon ScaleVolume 0";
};But it doesn't look like it is even getting that far. When I run the game with this, I get the warning:
Quote:
BehaviorComponent::addBehaviors - Missing Behavior SpawnPointBehavior TargetObject AIDragonTemplate
The unit spawns with the idle animation, but I can't control that unit at all because DragonClass::onAddToScene never gets called. SpawnPointBehavior is definitely there, there is even a .dso created for it when the game executes. I can also select it in the GUI under the behaviors tab.
Am I totally misdirected? Or am I close?
#3
Sorry but I have not used behaviors that much. I am not sure that you can put _Behavior0 in your datablock either.
What i did was copy the behavior tutorial. Instead of going through all that work, just use the Gamebuilder. Create your object with the behviors that you want. Move it off camera and name it "DragonClassTemplate" . When you want to spawn it, just call
That is the only way i have ever added behaviors. What is great is you don't have to worry about adding to scene either.
Hope that will work for you.
01/23/2009 (4:04 pm)
Hi Justin,Sorry but I have not used behaviors that much. I am not sure that you can put _Behavior0 in your datablock either.
What i did was copy the behavior tutorial. Instead of going through all that work, just use the Gamebuilder. Create your object with the behviors that you want. Move it off camera and name it "DragonClassTemplate" . When you want to spawn it, just call
%object = DragonClassTemplate.cloneWithBehaviors()
That is the only way i have ever added behaviors. What is great is you don't have to worry about adding to scene either.
Hope that will work for you.
#4
01/23/2009 (7:57 pm)
Thank you Andrew, you helped a lot.
Torque Owner Andrew Whittle
A t2dSceneObjectDatablock is more of a configuration. It describes the object but is not an object itself.
For instance, once you create your datablock
datablock t2dSceneObjectDatablock(ObjectData) { attribute = "1"; //this can be any attribute you want I just used //attribute };You can then add your object to the screen with the following:
%myObject = new t2dSceneObject() {config = "ObjectData";}; $myScene.addToScene(%myObject); //$myScene is any global variable you //have pointing to your SceneGraph //sceneWindow2D.getSceneGraph();You did not provide that much information so I can only assume that is what you are asking. Hope this helps.