Datablocks and Dynamic Object (SOLVED)
by Richard Petrov · in Torque Game Builder · 09/17/2011 (8:44 am) · 5 replies
I know that aStar works because I can place an object in TGB and assign it the class AIClass, superclass aStarActor. That works fine when I drag and drop it on the editor. Now I'm trying to load it dynamically with a datablock.
This is my code for testing purposes:
This is my code for testing purposes:
datablock t2dSceneObjectDatablock ( blackSpiderDatablock )
{
animationName = spider_west_anim;
class = AIClass;
superclass = aStarActor;
// size = "3.843, 3.843";
// Position = "-38.078, -33.078";
};
%bob = new t2dAnimatedSprite()
{
// class = AIClass;
//superclass = aStarActor;
scenegraph = sceneWindow2D.getSceneGraph();
config = blackSpiderDatablock;
};The animated spider shows up on the screen fine, but it doesn't follow any path set with aStar. I set the class and superclass up as what aStar requires, but nothing happens! About the author
#2
I tried your method by including the classes within the objects properties, but it still doesn't seem to want to work. The spider just sits there and animates, but doesn't follow a path. :-/
I wish there also were better docs for Datablocks because I had to read bits of pieces here and there to get to where I'm at. Which isn't far.
09/18/2011 (8:18 am)
Thanks I just used the [code] tags in my initial post. I tried your method by including the classes within the objects properties, but it still doesn't seem to want to work. The spider just sits there and animates, but doesn't follow a path. :-/
I wish there also were better docs for Datablocks because I had to read bits of pieces here and there to get to where I'm at. Which isn't far.
#3
After hours of searching and trying out new things what did it was this:
The onLevelLoaded() needed to be called. I don't understand why it doesn't run it automatically, but it works.
If anyone could explain to me why it would run that function automatically when it's in the TGB and why it doesn't when I create it dynamically that would be great.
09/19/2011 (7:41 am)
Well I got it working. I had my script in the games.cs file at the end of the StarGame() function.After hours of searching and trying out new things what did it was this:
echo ("Creating Bob...");
%bob0 = new t2dAnimatedSprite()
{
scenegraph = sceneWindow2D.getSceneGraph();
class = AIClass;
superclass = aStarActor;
config = blackSpiderDatablock;
};
%bob0.setSize("2,2");
%bob0.setPosition("-37.5, -33");
%bob0.onLevelLoaded();The onLevelLoaded() needed to be called. I don't understand why it doesn't run it automatically, but it works.
If anyone could explain to me why it would run that function automatically when it's in the TGB and why it doesn't when I create it dynamically that would be great.
#4
Edit: You might want to try going back to your original setup now that you got it working again, I might have been completely wrong about setting classes by datablock too. If that is the case, then at least that would make things easier.
09/19/2011 (1:37 pm)
Okay, I screwed up once so I hope I am not setting a trend here, but as I understand it, onLevelLoaded only gets called on objects during scene operations (like "loadLevel" and "addToLevel"). You can confirm that in the "~/common/gamescripts/levelManagement.cs" file. Likewise onLevelEnded should only get called when objects are removed in the respective functions.Edit: You might want to try going back to your original setup now that you got it working again, I might have been completely wrong about setting classes by datablock too. If that is the case, then at least that would make things easier.
#5
I also went back to my original setup with the class and superclass being in the Datablock and it works fine.
09/20/2011 (8:33 am)
Okay it makes a little more sense now. Thanks!I also went back to my original setup with the class and superclass being in the Datablock and it works fine.
Torque Owner Justin Proffitt
I hope that makes sense.. You could change it to the following:
datablock t2dSceneObjectDatablock ( blackSpiderDatablock ) { animationName = spider_west_anim; class = AIClass; superclass = aStarActor; // size = "3.843, 3.843"; // Position = "-38.078, -33.078"; }; %bob = new t2dAnimatedSprite() { class = blackSpiderDatablock.class; superclass = blackSpiderDatablock.superclass; scenegraph = sceneWindow2D.getSceneGraph(); config = blackSpiderDatablock; };That should work, though it isn't nearly so pretty.
Edit: Oh, also you can use [code] [/ code] tags to show your code in a friendlier format.
Post-Edit: For anyone else, this is wrong. Classes can be defined through datablocks just fine, you just cant change the class after having created the object.