Game Development Community

Creating objects for a level in script

by Nicolas Stohler · in Torque Game Builder · 08/10/2006 (2:06 pm) · 4 replies

The level editor is fine, but how do I create objects that I want to use in the level dynamically in script? I mean, for objects in the level (script class field filled in), I can use obj::onLevelLoaded(...) to setup this object. But where is the place to do it dynamically...and how do I get to the scenegraph for the level?

thanks

#1
08/10/2006 (4:10 pm)
Here is how you create a static sprite and an animated sprite

%sprite = new t2dStaticSprite()
{
   scenegraph = SceneWindow2D.getScenegraph();
};

%animSprite = new t2dAnimatedSprite()
{
   scenegraph = SceneWindow2D.getScenegraph();
};

Note In that example I store the object IDs in local variables so there will be no way to reference them out side of the function they're created in... you can pass a name in

%sprite = new t2dStaticSprite()
{
   scenegraph = SceneWindow2D.getScenegraph();
};

To then reference it as "testSprite", you can store it's ID in a global variable.

$sprite = new t2dStaticSprite()
{
   scenegraph = SceneWindow2D.getScenegraph();
};

So then you can reference it as $sprite... or you can store it's ID on an object ... so something like this

function TestClass::onLevelLoaded(%this, %scenegraph)
{
   %this.sprite = new t2dStaticSprite()
   {
      scenegraph = %scenegraph;
   };
}

now the new object's ID is stored on the object that the callback calls.
#2
08/11/2006 (1:18 am)
Thanks for your help

but where is the best place to do this? I will have several levels, so I will have to dynamically generate different objects depending upon which level is being loaded. is there some callback function for this or do I have to implement this functionality from scratch?
#3
08/11/2006 (1:20 am)
There are some typos in there. It should be:
[code]
%sprite = new t2dStaticSprite(testSprite)
{
scenegraph = SceneWindow2D.getScenegraph();
};
[code]
in the second code field and there is one % sign to many in the last one.
#4
08/17/2006 (1:16 pm)
I'm not quite sure I understand the solution here. I'm trying to do something similar; load tiles from a map file dynamically based on user input; not at load up time. Based on what I see above and in my .t2d file where other things get created, I should be able to do the following in my map.cs file:

$map01 = new t2dStaticSprite() {
	scenegraph = SceneWindow2D.getScenegraph();
	imageMap = "tilesgroundImageMap";
	frame = "1";
	position = "0 0";
	size = "15 13.8";
	layer= "3";
};

I don't get any errors at runtime, but the graphic doesn't show up either. Just for reference, I'm copying information from a tile I inserted with the editor:

new t2dStaticSprite() {
      imageMap = "tilesgroundImageMap";
      frame = "1";
      position = "11.5 -16.9";
      size = "15 13.8";
         mountID = "4";
   };

I tried changing the SceneWindow2D to the name of the variable at the top of the t2d file "levelContent" and that didn't work (threw an error), and I've added a mount="5"; (next number in the sequence) but that didn't seem to do anything. Any help?