Game Development Community

Spawning a TSStatic with script code

by Thijs Sloesen · 09/13/2004 (9:17 am) · 9 comments

First, we need a function to spawn the shape:

function createMyShape(%pos)  {
  %shape= new TSStatic() { shapeName = "~/data/shapes/blah/myshape.dts"; };
  MissionCleanup.add(%shape);
  %shape.setTransform(%pos);
  return %shape;
}

And last but not least a simple test function that we can call from the console to test our code and which spawns our shape at a random spawn point:
function testMyShape()  {
  return createMyShape(PickSpawnPoint());    
}

#1
09/17/2004 (6:51 pm)
Why not just simplify?


function createMyShape()
{
     %shape = new TSStatic()
      {
                 shapeName= "~/data/shapes/blah/myshape.dts";
      };
      MissionCleanup.add(%shape);

      %shape.setTransform(PickSpawnPoint());
}


Maybe even make it versitile....


function createMyShape(%shape)
{
      %create = new TSStatic()
      {
                 shapeName = "~/data/shapes/blah/" @ %shape @ ".dts";
      };
      MissionCleanup.add(%create);

      %create.setTransform(PickSpawnPoint());
}
#2
11/05/2004 (7:00 am)
Well, it's just an example, but if you prefer to code it that way, why not? :)
#3
06/02/2007 (5:45 am)
Can you explain the different lines of code to a newbie?

Thanks!
#4
08/05/2007 (7:15 am)
Sure thing.

function createMyShape(%shape)
This is the declaration of the function. 'function' tells the engine that this is a function and we want to be able to use it as such. 'createMyShape' is the function's name, and is what you use to call the function. %shape is what we call a parameter, and it can basically be anything you want. But if you want this function to actually work, it should be a string. "This is a string. Note the double quotation marks."

%cretate = new TSStatic()
Here, we create a new local variable called %create. Variables can hold anything - a string, an object, a number, whatever. This variable, %create, is being told to hold a new TSStatic. TSStatic is a simple class that just displays a shape file (.dts format). So we're creating a new one, and storing it in %create.

{
         shapeName = "~/data/shapes/blah/" @ %shape @ ".dts";
};
This little section gives the engine more information about the TSStatic we want to create. Critically, it tell the engine which DTS file to use. Notice that it's a string? The @ symbols here initially look confusing, but they're really simple. If you use an @ between two strings, it tells the engine to stick them together. So "Hi" @ "There" is equal to "HiThere". If you do instead "Hi " @ "There", you'd get "Hi There". There's an easier way to do this, though. "Hi" SPC "There" gives you "Hi There". Notice the difference?
Anyway, back on course. The shapeName string gives the location of the DTS file we want this TSSttic to look like. It will be in the folder [your mod]/data/shapes/blah, and the file name will be %shape .dts. See where %shape comes in now?
If you call this function like so:
createMyShape("tree");
Then shapeFile will be equal to:
"~/data/shapes/blah/tree.dts"
So you can use this to create any .dts file with any name in your blah folder by substituting "tree" in the function call for "[insert something here".

MissionCleanup.add(%create);
Remember %create is the TSStatic we just created? Basically, this tells the engine that the object is now part of the mision. You have to do this every time you create an object like this.

%create.setTransform(PickSpawnPoint());
%create still refers to the TSStatic we created. The setTransform method tells the engine to set the object's position and rotation to a specific value. If you wanted to set this manually, you'd do something like:
%create.setTransform("0 0 100 0 0 0 1");
The first three numbers in the string are the X/Y/Z location that you want to go to, and the latter four are for rotation. I don't really know how the rotation system works in Torque - I'd be happier with pan/tilt/roll :P.
PickSpawnPoint() is a function defined in the default scripts in starter.fps. It chooses one of the spawn points in the level, usually used for Players. It will return the location and rotation of one of those points, giving it directly to setTransform.

All clear? ;)
#5
08/17/2007 (6:11 am)
Excellent! Thanks.

Tony
#6
12/20/2007 (12:21 pm)
great script and thanks for taking the time to explain it as well

kevin
#7
10/04/2008 (7:41 am)
Thanks, I was looking for this.
#8
10/22/2008 (1:09 pm)
#9
10/22/2008 (3:29 pm)
Can anyone explain the difference between a "new TSStatic()" and "new StaticShape()" if there is a difference?