Game Development Community

Problem creating a class (C++)

by Sergio Seligmann Rodrigues · in Torque 3D Professional · 04/20/2011 (4:19 pm) · 1 replies

Hi

I trying to create a class in the engine, I use the ShapeBase.h ShapeBase.cpp as model. Then I create a datablock using my class. In the world editor appears my data block but when I create a instance my torque crash sometimes is when I interact with.

Tnx in advance

.h

#ifndef _NODE_H_
#define _NODE_H_

#ifndef _SHAPEBASE_H_
#include "T3D/shapeBase.h"
#endif

struct NodeData: public ShapeBaseData 
{
   typedef ShapeBaseData Parent;

   NodeData();

   S32   dynamicTypeField;

   DECLARE_CONOBJECT(NodeData);

   static void initPersistFields();
   virtual void packData  (BitStream* stream);
   virtual void unpackData(BitStream* stream);
};

class Node : public ShapeBase
{
	typedef ShapeBase Parent;

public:

	Node(void);
	Node(int x, int y);
	~Node(void);

	DECLARE_CONOBJECT(Node);

	bool onAdd();
    void onRemove();
	bool onNewDataBlock(GameBaseData* dptr);

	void processTick(const Move *move);
    void interpolateTick(F32 delta);
    void setTransform(const MatrixF &mat);

	void onUnmount(ShapeBase* obj,S32 node);

	U32  packUpdate  (NetConnection *conn, U32 mask, BitStream *stream);
    void unpackUpdate(NetConnection *conn,           BitStream *stream);

	static void initPersistFields();

	void  setX(int x);
	int   getX();
	
	void  setY(int y);
	int   getY();
	
	void  setF(int f);
	int   getF();
	
	void  setG(int g);
	int   getG();
	
	void  setH(int h);
	int   getH();
	
	void  setUniqueN(int uniqueN);
	int   getUniqueN();
	
	void  setForward(Node *nodeToSet);
	Node* getForward();
	
	void  setBackward(Node *nodeToSet);
	Node* getBackward();

protected:

   enum MaskBits 
   {
      PositionMask = Parent::NextFreeMask,	  
      NextFreeMask = Parent::NextFreeMask << 1
   };

private:

	NodeData* mDataBlock;

	int   x;
	int   y;
    int   f;
    int   g;
    int   h;
    int   uniqueN;

    Node *nodeForward;
	Node *nodeBackward;
};

#endif

.cpp

#include "Node.h"

#include "core/stream/bitStream.h"
#include "math/mathIO.h"
#include "console/consoleTypes.h"

//IMPLEMENT_CONOBJECT(Node);

static const U32 sgAllowedDynamicTypes = 0xffffff;

IMPLEMENT_CO_DATABLOCK_V1 (NodeData);
NodeData::NodeData()
{
}

void NodeData::initPersistFields()
{ 
	addField("dynamicType",          TypeS32,  Offset(dynamicTypeField,     NodeData));

	Parent::initPersistFields(); 
}

void NodeData::packData(BitStream* stream)
{ 
	Parent::packData(stream); 
	
	stream->write(dynamicTypeField);
}

void NodeData::unpackData(BitStream* stream)
{ 
	Parent::unpackData(stream); 

	stream->read(&dynamicTypeField);
}

IMPLEMENT_CO_NETOBJECT_V1(Node);
Node::Node(void)
{
	this->x            = 0;
    this->y            = 0;
    this->f            = 0;
    this->g            = 0;
    this->h            = 0;
    this->uniqueN      = 0;
}

Node::Node(int x, int y)
{
	this->x            = x;
    this->y            = y;
    this->f            = 0;
    this->g            = 0;
    this->h            = 0;
    this->uniqueN      = 0;
	this->nodeForward  = new Node();
	this->nodeBackward = new Node();
}

Node::~Node(void){ }

void Node::initPersistFields()
{ 
    addGroup("Lighting");
    addField("receiveSunLight", TypeBool, Offset(receiveSunLight, SceneObject));
    addField("receiveLMLighting", TypeBool, Offset(receiveLMLighting, SceneObject));
    //addField("useAdaptiveSelfIllumination", TypeBool, Offset(useAdaptiveSelfIllumination, SceneObject));
    addField("useCustomAmbientLighting", TypeBool, Offset(useCustomAmbientLighting, SceneObject));
    //addField("customAmbientSelfIllumination", TypeBool, Offset(customAmbientForSelfIllumination, SceneObject));
    addField("customAmbientLighting", TypeColorF, Offset(customAmbientLighting, SceneObject));
    addField("lightGroupName", TypeRealString, Offset(lightGroupName, SceneObject));
    endGroup("Lighting");

	Parent::initPersistFields(); 
}

bool Node::onAdd()
{
   if(!Parent::onAdd() || !mDataBlock)
      return false;

   // We need to modify our type mask based on what our datablock says...
   mTypeMask |= (mDataBlock->dynamicTypeField & sgAllowedDynamicTypes);

   addToScene();

   if (isServerObject())
      scriptOnAdd();
   return true;
}

bool Node::onNewDataBlock(GameBaseData* dptr)
{
   mDataBlock = dynamic_cast<NodeData*>(dptr);
   if (!mDataBlock || !Parent::onNewDataBlock(dptr))
      return false;

   scriptOnNewDataBlock();
   return true;
}

void Node::onRemove()
{
   scriptOnRemove();
   removeFromScene();
   Parent::onRemove();
}

void Node::processTick(const Move* move)
{
   Parent::processTick(move);

   // Image Triggers
   if (move && mDamageState == Enabled) 
   {
      setImageTriggerState(0,move->trigger[0]);
      setImageTriggerState(1,move->trigger[1]);
   }

   if (isMounted())
   {
      MatrixF mat;
      mMount.object->getMountTransform(mMount.node,&mat);
      Parent::setTransform(mat);
      Parent::setRenderTransform(mat);
   }
}

void Node::interpolateTick(F32)
{
   if (isMounted()) 
   {
      MatrixF mat;
      mMount.object->getRenderMountTransform(mMount.node,&mat);
      Parent::setRenderTransform(mat);
   }
}

void Node::setTransform(const MatrixF& mat)
{
   Parent::setTransform(mat);
   setMaskBits(PositionMask);
}

void Node::onUnmount(ShapeBase*,S32)
{
   // Make sure the client get's the final server pos.
   setMaskBits(PositionMask);
}

U32 Node::packUpdate(NetConnection *connection, U32 mask, BitStream *bstream)
{
   U32 retMask = Parent::packUpdate(connection,mask,bstream);
   
   if (bstream->writeFlag(mask & PositionMask | ExtendedInfoMask))
   {
      mathWrite(*bstream,mObjToWorld);
      mathWrite(*bstream, mObjScale);
   }

   if (mLightPlugin) 
   {
      retMask |= mLightPlugin->packUpdate(this, ExtendedInfoMask, connection, mask, bstream);
   }

   return retMask;
}

void Node::unpackUpdate(NetConnection *connection, BitStream *bstream)
{
   Parent::unpackUpdate(connection,bstream);

   if (bstream->readFlag())
   {
      MatrixF mat;
      mathRead(*bstream,&mat);
      Parent::setTransform(mat);
      Parent::setRenderTransform(mat);

      VectorF scale;
      mathRead(*bstream, &scale);
      setScale(scale);
   }

   if (mLightPlugin)
   {
      mLightPlugin->unpackUpdate(this, connection, bstream);
   }
}

void  Node::setX(int x){ this->x = x; }

int   Node::getX(){ return this->x; }

void  Node::setY(int y){ this->y = y; }

int   Node::getY(){ return this->y; }

void  Node::setF(int f){ this->f = f; }

int   Node::getF(){ return this->f; }

void  Node::setG(int g){ this->g = g; }

int   Node::getG(){ return this->g; }

void  Node::setH(int h){ this->h = h; }

int   Node::getH(){ return this->h; }

void  Node::setUniqueN(int uniqueN){ this->uniqueN = uniqueN; }

int   Node::getUniqueN(){ return this->uniqueN; }

void  Node::setForward(Node *nodeForward){ this->nodeForward = nodeForward; }

Node* Node::getForward(){ return this->nodeForward; }

void  Node::setBackward(Node *nodeBackward){ this->nodeBackward = nodeBackward; }

Node* Node::getBackward(){ return this->nodeBackward; }

ConsoleMethod(Node, setX, void, 3, 3, "obj.setX(newX)")
{
   TORQUE_UNUSED(argc);
   U32 value = U32(dAtof(argv[2]));
   object->setX(value);
}

ConsoleMethod(Node, getX, S32, 2, 2, "obj.getX()")
{
   TORQUE_UNUSED(argc); TORQUE_UNUSED(argv);
   return object->getX();
}

datablock

datablock NodeData(NodeAI)
{  	
   className = "NodeAI";
   category = "AI";
   shapeFile = "art/shapes/rocks/boulder.dts";
   numberOfNeighbors = 0;
   neighbourhood = 0;
};

function NodeData::Create(%block)  
{  
   %obj = new Node(){ dataBlock = %block; };  
   
   echo("Node created - " @ "Datablock: " @ %block);   
   
   return(%obj);  
}

function NodeAI::onCollision(%this,%obj,%col)
{
	//%obj.setX(10);
	//echo(%obj.getX());

	//%obj.schedule(250, asd, %obj);
}

function NodeAI::addNeighbor(%this,%obj)
{
   %this.neighbourhood[%this.numberOfNeighbors] = %obj;
   %this.numberOfNeighbors++;      
      
   echo(%this.getName() @ " - Neighbor - " @ %obj.getName() @ " - ADD");
}

#1
04/21/2011 (11:04 am)
Sergio, you might be hitting the netMask limit..the following thread talks about SimObjectTypes, but also mentions netmask flags being near the limit also. You may want to extend shapebase instead of writing a brand new class. Someone more knowledgeable may be able to step in and verify.
http://www.garagegames.com/community/forums/viewthread/125405