Game Development Community

Returning a class object

by Stephen Lujan · in Torque Game Engine · 06/02/2007 (3:48 pm) · 3 replies

My new class is a big complex object which I plan on adding to the aiplayer class. I just have one simple question.

To access this new object from the script is it more cpu efficient to make it a persistant field, or to make a console method which returns another function which returns the new object.

meaning
void PlayerData::initPersistFields()
{
   Parent::initPersistFields();
   addField("theNewObject", TypeNewObjectClass, Offset(NewObject, AIPlayer));
//.....
or
const Aiplayer::getnewobject()
{
   return this->mNewThingamajig;
}

ConsoleMethod( aiplayer, getNewObject, newobject, 2, 2, "Does this even work like this in torque?")
{
   return object->getNewObject();
}

Basically all I know is I need to pass it as a reference because its big, but I'm not a c++ or tge guru, so I'm not sure if these methods do that.

#1
06/02/2007 (4:13 pm)
They are pretty much the same thing. addField is a macro that will essentially create the console methods for you (get and set). If you are not doing anything special besides getting/setting the object then you can just stick with initPersitFields.

You also shouldn't need a new object type in the add field. Using a TypeSimObjectPtr will work as long as your class is derived from SimObject.
#2
06/02/2007 (6:52 pm)
Thanks Peter. I tried both ways and found that I actually couldn't get either to work now.

My class is derived from a simObject, but I tried using TypeSimObjectPtr and it always returns two little square symbols and an S when i echo the field to the console. The member i want to pass isn't a pointer though, its a whole object. I just want to pass the ID for the object to the scripts.

I also tried the below and it always returned 0.
//in the header
S32 getCombatBrainID() const { return mCombatBrain.getId(); }

//in the .cc
ConsoleMethod( AIGuard, getCombatBrain, S32, 2, 2, "()"
              "Gets the ai's combat brain")
{
   return object->getCombatBrainID();
}
#3
06/03/2007 (7:38 pm)
After looking at the simobject code I realized that by adding my new object to an existing class, I was probably avoiding a call to the simobject function registerObject, which meant it never received ID. I added registerObject(); to the constructor for my new class and used the simplified code below to access it from my ai object.
ConsoleMethod( AIGuard, getCombatBrain, S32, 2, 2, "()"
              "Gets the ai's combat brain")
{
	argc; argv;
   SimObject *obj = &object->mCombatBrain;
   if(!obj)
      return -1;
   return obj->getId();
}
Now I can finally access it from the script side. Its a shame I couldn't use persistent fields, but at least I have it working.