Game Development Community

How to create instance of simdatablock?

by Johnathan Moore · in Torque Game Engine · 03/17/2006 (4:53 pm) · 1 replies

Hi,
Could someone tell me who I could create a simple simdatablock instance in engine code I am new to modifying the engine code and not very good at c++. I have tried this is what I gathered from a tutorial on creating a simobject. If there is a tutorial I have missed then could someone point me to it.

header
rtssquad.h
//-----------------------------------------------------------------------------
// Torque Game Engine 
//-----------------------------------------------------------------------------

#ifndef _RTSSquadData_H_
#define _RTSSquadData_H_

#ifndef _SIMBASE_H_
#include "console/simBase.h"
#endif

class RTSSquadData : public SimDataBlock
{
   // This typedef is required for tie ins with the script language.
   //--------------------------------------------------------------------------
	protected:
      typedef SimDataBlock Parent;
   //--------------------------------------------------------------------------

   public:
      RTSSquadData();
      ~RTSSquadData();

      // These are overloaded functions from SimObject that we handle for
      // tie in to the script language.  The .cc file has more in depth
      // comments on these.
      //-----------------------------------------------------------------------
      bool processArguments(S32 argc, const char **argv);
      bool onAdd();
      void onRemove();
      static void initPersistFields();
      //-----------------------------------------------------------------------

      void setVariable(int var);
      int sum(int a, int b);
      void doCallback();
   
   private:
      int mMemberVar;      
int herohandle;
int leaderhandle;

   // This macro ties us into the script engine, and MUST MUST MUST be declared
   // in a public section of the class definition.  If it isn't you WILL get
   // errors that will confuse you.
   //--------------------------------------------------------------------------
   public:
   DECLARE_CONOBJECT(RTSSquadData);
   //--------------------------------------------------------------------------
};

class RTSSquad : public SimObject
{
   // This typedef is required for tie ins with the script language.
   //--------------------------------------------------------------------------
	protected:
      typedef SimDataBlock Parent;
   //--------------------------------------------------------------------------


}
#endif // _RTSSquadData_H_

#1
03/17/2006 (4:53 pm)
Source
RTSSquad.cc
//-----------------------------------------------------------------------------
// Torque Game Engine 
//-----------------------------------------------------------------------------
#ifndef _RTSSQUAD_H_
#include "game/RTS/RTSSquad.h"


#ifndef _SIMBASE_H_
#include "console/simBase.h"
#endif

#ifndef _CONSOLEINTERNAL_H_
#include "console/consoleInternal.h"
#endif

// This macro is required in order to "tie in" with the scripting engine.
IMPLEMENT_CO_DATABLOCK_V1(RTSSquadData);

// Just your standard object constructor.  Nothing fancy here.
RTSSquadData::RTSSquadData()
{
   mMemberVar = 0;
}

// Just your standard object destructor.  Nothing fancy here.
RTSSquadData::~RTSSquadData()
{

}

// This is the first of the functions which deals with the script system.
// This one is actually almost never used by other Torque objects.  In fact
// only one object currently uses it and that is TCPObject.  To the best of
// my knowledge anyway.  This is similiar to arguments when running Torque.
// If you create a new instance of this object and specify "arguments" 
// furing the creation liek this:
// 
// $object = new RTSSquadData(Name, argument1, argument 2);
//
// argument1, and argument2 would get passed into this function as
// arguments.  As I said its rarely used but I included it here
// for completeness.
bool RTSSquadData::processArguments(S32 argc, const char **argv)
{
   if(argc == 0)
      return true;
   else 
      return true;

   return false;
}

// This is a very important function.  Basically this is called when
// an instance of your object is created and registered by the script
// engine.  What we do here is first call our parent's onAdd() and
// return false if it fails.  Next, we find out what the script user
// "named" this instance of our object and create a valid namespace
// for that name.  This is important and is what allows the script
// user to write script like this:
//
// $object = new RTSSquadData(myObject);
// myObject::onCallback(%this, %var)
// {
// };
//
// In the above example, "myObject" is what the script user named 
// the object instance.  Without the code below, the function above
// would have to be RTSSquadData::onCallback().
bool RTSSquadData::onAdd()
{
   if (!Parent::onAdd())
      return false;

   const char *name = getName();
   if(name && name[0] && getClassRep())
   {
      Namespace *parent = getClassRep()->getNameSpace();
      Con::linkNamespaces(parent->mName, name);
      mNameSpace = Con::lookupNamespace(name);
   
   }

   return true;
}

// This is the function that gets called when an instance
// of your object is being removed from the system and being
// destroyed.  Use this to do your clean up and what not.
void RTSSquadData::onRemove()
{
   Parent::onRemove();
}

// To be honest i'm not 100% sure on when this is called yet.
// Basically its used to set the values of any persistant fields
// the object has.  Similiar to the way datablocks work.  I'm
// just not sure how and when this gets called.
void RTSSquadData::initPersistFields()
{
   Parent::initPersistFields();
}

//-----------------------------------------------------------------------
// These functions below are our custom functions that we will tie into
// script.  They are all pretty self explanatory except for doCallback()
// which executes a scrpit function named "onCallback".
void RTSSquadData::setVariable(int var)
{
   mMemberVar = var;
}

int RTSSquadData::sum(int a, int b)
{
   return a + b;
}

void RTSSquadData::doCallback()
{
   Con::executef(this, 2, "onCallback", "string_variable");
}

//-----------------------------------------------------------------------
// These functions are the code that actually tie our object into the scripting
// language.  As you can see each one of these is called by scrpit and in turn
// calls the C++ class function.
ConsoleMethod(RTSSquadData, setVariable, void, 3, 3, "(int variable) Sets a variable in the RTSSquadData")
{
   object->setVariable(dAtoi(argv[2]));
}

#endif // _RTSSQUAD_H_


Error 58 error C2236: unexpected 'enum' 'ConsoleDynamicTypes'. Did you forget a ';'? c:\enginemake\engine\console\consoleTypes.h 17
Error 59 error C2143: syntax error : missing ';' before '{' c:\enginemake\engine\console\consoleTypes.h 17
Error 60 error C2447: '{' : missing function header (old-style formal list?) c:\enginemake\engine\console\consoleTypes.h 17
Error 61 error C2065: 'TypeF32' : undeclared identifier c:\enginemake\engine\console\consoleInternal.h 180
Error 62 error C2065: 'TypeS32' : undeclared identifier c:\enginemake\engine\console\consoleInternal.h 182