Game Development Community

Question: spawning objects.

by __._ · in Torque Game Engine · 09/29/2006 (4:53 am) · 6 replies

Good day all,

I've only had time to experiment with TGE for rougly 4 days now, but have read most of the manual, created some test worlds and dif-models. Also did the Getting Started. No problem yet.

But now that I'm starting to experiment with my own (unguided) stuff, I immediately get stuck. I'm trying to modify the getting started to spawn a new logo when I hit a logo I placed with the editor. The collision function is called fine, but I seem unable to create a new logo through script. The datablock already exists and accordig to the manual I only need to call new StaticShape() with the correct vallues (datablock name, position etc) to create a new one, but nothing is happening.

Where should this code go?

And how would I go about modifying objects, e.g. place this new object near the player location, have something spin over time, etc ?

Thanks in advance.

#1
09/29/2006 (2:42 pm)
I have this code in server.cs:
function makeAThing()
{
   echo("makeAThing() called");
  %thing = AIPlayer::spawnOnPath("thing", "MissionGroup/Paths/ThingPath");
  if (!%thing) {
   echo("Error spawning thing (probably path not found)\n");
   return;
  }
  %thing.setMoveSpeed(0.5);
  %thing.pushTask("followPath(\"MissionGroup/Paths/ThingPath\",-1)");
  %thing.pushTask("animate(\"run\")");
  MissionCleanup.add(%thing);
   echo("Spawned a thing.");
}

It is triggered in onMissionLoaded(), but I imagine that any other event could trigger it, too.

You can change AIPlayer to some other object class instance to create something simpler.

In general, if you look in the mission file, it's just a TorqueScript, so you can duplicate whatever is done in there to create the instances.
#2
09/30/2006 (12:58 am)
I got it working a little while after I posted it (tried to remove the thread but couldn't) but thanks. The more code I see the more I learn.

I did do it different though... but don't have the code with me right now.

Is there an overview of functions somewhere that I missed?
#3
10/02/2006 (11:07 am)
No, what you used is probably fine. The example above doesn't really have anything to do with spawning an object. The relevant code would be inside the spawnOnPath probably, though it would probably include things that are more complex than needed for just spawning a static object. The main point to spawning an object in script is using the "new" operator and then defining a class of object to spawn and giving it necessary data (datablocks, position, etc.) for what it needs to function properly. The only important bit in that code is the "MissionCleanup.add(%thing);" line, as you want to add any objects you spawn to that group, so they are deleted when a mission ends.
#4
10/02/2006 (2:58 pm)
AIPlayer is part of the standard sample code. Here's the actual spawning:

function AIPlayer::spawn(%name,%spawnPoint)
{
   // Create the demo player object
   %thing = new AiPlayer() {
      dataBlock = SomeDataBlock;
   };
   MissionCleanup.add(%thing);
   %thing.setShapeName(%name);
   %thing.setTransform(%spawnPoint);
   return %thing;
}

Again, you can do something simpler for a static object. Also, if you look at that first tutorial, the mission file will have code that creates the Torque logo instances. You can copy that code out of the mission file, and run it as part of the server-side collision code, to spawn new logos.
#5
10/02/2006 (11:12 pm)
So what if I want my objects to be dynamic? (if they can be? have gravity etc.?) Right now im using StaticShape.
#6
10/03/2006 (11:37 am)
In 1.4.2 there's a new class called rigidShape. It's not in 1.4.0 AFAICT. You can get 1.4.2 by getting the SDK from the CVS repository.

Note: Torque is not a full-fledged physics simulator like you'd get from Ageia (PhysX) or ODE. You could, of course, get one of those libraries, and integrate with Torque if you wanted more physical behavior. There might even be resources to do this already that you can integrate.