Game Development Community

SimObject stuff

by Gary "ChunkyKs" Briggs · in Torque Game Engine · 05/17/2005 (2:12 pm) · 7 replies

So I'm being big and brave and creating my first simobject-derived thing.

www.garagegames.com/docs/tge/engine/classSimObject.php

Says the following:
Quote:Registering a SimObject performs these tasks: Calls the object's onAdd() method.

I don't get it. If I was going to register an object, it would make sense to me [given my limited torque knowlege] to do it in the onAdd function. But if registering an object calls it's onAdd function, then clearly I have the wrong place. Should I use the constructor to do it? I don't see many simobject-derived things using a constructor at all...

What's the correct way to do this? Also, outside of adding that if(!parent:onadd()) bit, what other requirements are there for me to add to the class so that I can use it from torquescript, something like;

%foo = new simobjectderivedthing(%blather);
%foo.dostuff(%snurgles);

I already have a whole bunch of ConsoleMethods set up, but I'm not clear on how to make sure that Torque knows to make them available, etc...

Also, can I pass things to a constructor [ie, that %blather thing above], and if so, how? And how does the id get returned from the constructor?

Gary (-;

#1
05/18/2005 (10:34 am)
Actually, the documentation is just a touch misleading--the registerObject() call is only for objects that are instantiated from c++ code: it does not need to be used when you are writing a class that inherits from SimObject or below.

The IMPLEMENT_xxx macros that are part of your class implementation handle registering the class itself to the console (AbstractClassRep work), and the new operator is what makes sure that your object, when created in script by using an implemented class, is properly registered. The new operator itself is what returns the objectID handle.

If you search through the TGE source code for "registerObject", you'll see some of the why's and how's it is used: specifically, look at footpuff/footprint, etc., and you'll get a feel for how it works.
#2
05/18/2005 (11:44 am)
Yah, eventually I found that stuff. Saliently, it would have helped if I'd read the documentation for consoleObject in addition to the documentation about SimObject.

anyways. Got all that working, as can be seen here:

==>$foo = new tdBody();
==>$foo.dump();
Member Fields:
Tagged Fields:
Methods:
  AddForce() - (fx,fy,fz) Add a Force to a Body
  AddForceAtPos() - (fx,fy,fz,px,py,pz) Add a Force to a Body at a specific Position
  AddForceAtRelPos() - (fx,fy,fz,px,py,pz) Add a Force to a Body at a Relative Position
  AddRelForce() - (fx,fy,fz) Add a Relative Force to a Body
  AddRelForceAtPos() - (fx,fy,fz,px,py,pz) Add a Relative Force to a Body at a Position
  AddRelForceAtRelPos() - (fx,fy,fz,px,py,pz) Add a Relative Force to a Body at a Relative Position
  AddRelTorque() - (fx,fy,fz) Add a Relative Torque to a Body
  AddTorque() - (fx,fy,fz) Add a Torque to a Body
  Create() - (world) Create Body in World
  dBodyGetFiniteRotationAxis() - () Get Body's Finite Rotation Axis
  delete() - obj.delete()
  Disable() - () Disable Body
  dump() - obj.dump()
  Enable() - () Enable Body
  GetAngularVel() - () Get Body's Angular Velocity
  getClassName() - obj.getClassName()
  GetFiniteRotationMode() - () Get a body's finite rotation mode
  GetForce() - () Get Body's Force
  GetGravityMode() - () Get a body's gravity mode
  getGroup() - obj.getGroup()
  getId() - obj.getId()
  GetLinearVel() - () Get Body's Linear Velocity
  getName() - obj.getName()
  GetNumJoints() - () Get a number of joints on a body
  GetPosition() - () Get Body's Position
  GetPosRelPoint() - (px,py,pz) Get Body's Relative Position
  GetRelPointPos() - (px,py,pz) Get Body's Relative Position
  GetRelPointVel() - (px,py,pz) Get Body's Relative Velocity
  GetTorque() - () Get Body's Torque
  GetType() - obj.getType()
  IsEnabled() - () See if body is Enabled
  save() - obj.save(fileName, <selectedOnly>)
  schedule() - object.schedule(time, command, <arg1...argN>);
  SetAngularVel() - (x,y,z) Set Body's Angular Velocity
  SetFiniteRotationAxis() - (x,y,z) Set Body's Finite Rotation Axis
  SetFiniteRotationMode() - (mode) Set a body's finite rotation mode
  SetForce() - (x,y,z) Set Body's Force
  SetGravityMode() - (mode) Set a body's gravity mode
  SetLinearVel() - (x,y,z) Set Body's Linear Velocity
  setName() - obj.setName(newName)
  SetPosition() - (x,y,z) Set Body's Position
  SetTorque() - (x,y,z) Set Body's Torque
  VectorFromWorld() - (px,py,pz) Get Body's Vector from World
  VectorToWorld() - (px,py,pz) Get Body's Vector to World

Thank-you very much,
Gary (-;
#3
05/18/2005 (11:53 am)
Constructor: new Foo(ObjectName, %firstArg, %secondArg);

Registering is an action done by the engine on your object, not something you (except in a few rare cases) will ever do yourself. Definitely not something your object should be doing on itself! :)
#4
05/18/2005 (12:07 pm)
ObjectName?

And does this mean that Torque magically does the right things with regards to parameter passing, and that I *don't* need a ConsoleMethod or anythign to make it work?

Thanks,
Gary (-;
#5
05/18/2005 (4:36 pm)
Every object can have a global name, which is what is specified by the first parameter to the constructor. Yes, it's a bit odd, but in practice it works really well.

I'm not sure what the second part of your post is talking about.
#6
05/18/2005 (5:02 pm)
You have two basic options tying class implementation variables to script, and the other way 'round:

AddField (in initPersistFields): this basically links variables directly from the class object instantiation to the console, allowing things like changing an object instantiation's variable directly via script (%myObject.MyPersistentField = 10)

ConsoleMethod: Alternatively, you can create accessor methods in your object for the various variables, and then implement ConsoleMethods that provide a "handle" to the scripts to call this functionality. Additionally, ConsoleMethods can obviously be used to perform various tasks.

In general, what is important to understand about registerObject is:

1) It only needs to be performed manually for objects that are created in source code (for example, footpuffemitters use it).
2) All it does is to make sure that the object is considered part of the simulation space (given a unique ObjectID, can use the various methods SimObjects have appropriately, such as delete notification, etc).
3) If you are creating objects in script (by using the new (myObjectClass) operator), then simulation registering is done for you.


And, as I said above, to have objects able to "pass" information back and forth between your scripts and the underlying class implementation, you need to use persistent fields or console methods.
#7
05/18/2005 (5:11 pm)
Ah, thank-you. In the end, I did the AddField thing. Now, to initialise a body:

$w = new tdWorld();
$b = new tdBody() { simworld = $w; };

Thanks,
Gary (-;