Game Development Community

Dynamic Object creation

by Anthony Rosenbaum · in Torque Game Builder · 03/26/2009 (10:04 am) · 1 replies

I am having troube understanding how I might create a object to the scene dynamically.

For instance, I have a Behavior that defines what and object does, I have a template that assigns the behavior to an object. And I have a class that will create and attach the template to the object.

But if I have a managerclass that creates ObjectX on the fly, it either doesn't work or crashes.

Could someone give me some clue to what I am missing here is general example of what I am talking about

behavior
if (!isObject(BobBehavior))
{
   %template = new BehaviorTemplate(BobBehavior); 
   %template.friendlyName = "BobBehavior";
   %template.behaviorType = "Bob";
   %template.description  = "Bob can be clicked";
}

function BobBehavior::onBehaviorAdd(%this)
{
   //use mouse events
   %this.owner.setUseMouseEvents(true);
}

Template
datablock t2dSceneObjectDatablock(BobTemplate)
{
	Class				= "BobClass";
	Layer				= "1";
	_Behavior0			= "BobBehavior";
};

Class
function BobClass::onAddToScene(%this)
{

}

function BobClass::onRemove(%this)
{
	if (isObject(%this.Bob))
		%this.Bob.safeDelete();
}

Manager

if (isObject(BobManager))
   BobManager.delete();

new ScriptObject(BobManager){};

function BobManager::onAdd(%this)
{
    %this.bobSet = new SimSet();
}
function BobManager::addBob(%this)
{

   %bob = new t2dSceneObject(){
      scenegraph = $scenegraph;
      class = BobClass;
   };
   %bob.setPosition("5.896 -1.871");
   %this.bobSet.add(%bob);

}



SOLVED, Coded is changed to reflect some usefulness
Note $scenegraph is initalized from changing
sceneWindow2D.loadLevel(%level);
To
$scenegraph = sceneWindow2D.loadLevel(%level);

in StartGame

#1
03/26/2009 (11:46 am)
It doesn't seem like you're setting the datablock for %bob
try
%bob = new t2dSceneObject(){
      config = "BobTemplate";
      }
%bob.addToScene($scenegraph);