Datablock C++... stuff
by Gary "ChunkyKs" Briggs · in Torque Game Engine · 08/25/2006 (5:40 pm) · 2 replies
Here's some [quite a lot] of code:
jmtorque.h:
jmtorque.cc:
And then create datablocks:
JugglingBall is just a generic shapeBase-derived object, nothing fancy.
Problems:
1) When I try to connect to a server with those datablocks created, the client just locks.
With just the JugglingBallData, all is fine [hence I left the code out], but if I try to connect to one with JMTorqueData(three) created, then the client locks right at the end of the "loading datablocks" progress bar. I can still drop down a console or click cancel on the loading screen, but the loading screen is near the end of the progress bar and never moves again.
2) I'm pretty sure my string stuff is very pathologically broken, but I can't find good examples of how I'd implement strings in datablocks. "category" in gameBase seemed like a good bet, but doesn't seem to get shifted around the network properly.
3) jmbd.getId() doesn't feel right, somehow, but just setting ball_data=jmbd; leaves ball_data set to zero. What would be a better way of having a field in a datablock point to another datablock?
4) JML_CHAR * for my strings is probably strange. I see StringTableEntry normally used, but trying that didn't seem to make much of a difference. I just set it to JML_CHAR * to save myself some typecasting trauma later.
I strongly suspect that this code is broken in a whole plethora of ways but I don't really know what those ways are :-/
It's important that styles, pattern names, and siteswaps are strings.
Please could someone with a good working knowlege of C++ datablock stuff explain and help out?
Thanks,
Gary (-;
jmtorque.h:
struct JMTorqueData : public GameBaseData {
typedef GameBaseData Parent;
public:
DECLARE_CONOBJECT(JMTorqueData);
JMTorqueData();
~JMTorqueData();
static void consoleInit();
static void initPersistFields();
bool preload(bool server, char errorBuffer[256]);
virtual void packData(BitStream* stream);
virtual void unpackData(BitStream* stream);
JML_CHAR *siteswap; // StringTableEntry didn't make a difference.
JML_CHAR *pattern_name;
JML_CHAR *style;
S32 bdataid; // ID of a datablock
};jmtorque.cc:
IMPLEMENT_CO_DATABLOCK_V1(JMTorqueData);
JMTorqueData::JMTorqueData()
{
siteswap="3";
pattern_name="3 Balls";
style="Normal";
bdataid=0;
}
JMTorqueData::~JMTorqueData()
{
// Should I delete the strings, here?
// I allocate them in unpackData, otherwise they're not allocated by me
// If they're set using the script-available fields [ie at the datablock creation in script], does Torque manage this for me?
}
void JMTorqueData::consoleInit()
{
Parent::consoleInit();
}
void JMTorqueData::initPersistFields()
{
Parent::initPersistFields();
addField("siteswap", TypeString, Offset(siteswap, JMTorqueData));
addField("pattern_name", TypeString, Offset(pattern_name, JMTorqueData));
addField("style", TypeString, Offset(style, JMTorqueData));
addField("ball_data", TypeS32, Offset(bdataid, JMTorqueData));
}
bool JMTorqueData::preload(bool server, char errorBuffer[256])
{
return Parent::preload(server, errorBuffer);
}
void JMTorqueData::packData(BitStream* stream)
{
Parent::packData(stream);
stream->writeString(siteswap,dStrlen(siteswap));
stream->writeString(pattern_name,dStrlen(pattern_name));
stream->writeString(style,dStrlen(style));
stream->writeInt(bdataid, 32);
}
void JMTorqueData::unpackData(BitStream* stream)
{
char buf[256];
Parent::unpackData(stream);
stream->readString(buf);
siteswap = dStrdup(buf);
stream->readString(buf);
pattern_name = dStrdup(buf);
stream->readString(buf);
style = dStrdup(buf);
bdataid = stream->readInt(bdataid);
}And then create datablocks:
datablock JugglingBallData(jmbd) {
shape="~/data/shapes/ball/ball.dts";
};
datablock JMTorqueData(three) {
ball_data=jmbd.getId();
siteswap="3";
style="Normal";
pattern_name="Three Balls";
};JugglingBall is just a generic shapeBase-derived object, nothing fancy.
Problems:
1) When I try to connect to a server with those datablocks created, the client just locks.
With just the JugglingBallData, all is fine [hence I left the code out], but if I try to connect to one with JMTorqueData(three) created, then the client locks right at the end of the "loading datablocks" progress bar. I can still drop down a console or click cancel on the loading screen, but the loading screen is near the end of the progress bar and never moves again.
2) I'm pretty sure my string stuff is very pathologically broken, but I can't find good examples of how I'd implement strings in datablocks. "category" in gameBase seemed like a good bet, but doesn't seem to get shifted around the network properly.
3) jmbd.getId() doesn't feel right, somehow, but just setting ball_data=jmbd; leaves ball_data set to zero. What would be a better way of having a field in a datablock point to another datablock?
4) JML_CHAR * for my strings is probably strange. I see StringTableEntry normally used, but trying that didn't seem to make much of a difference. I just set it to JML_CHAR * to save myself some typecasting trauma later.
I strongly suspect that this code is broken in a whole plethora of ways but I don't really know what those ways are :-/
It's important that styles, pattern names, and siteswaps are strings.
Please could someone with a good working knowlege of C++ datablock stuff explain and help out?
Thanks,
Gary (-;
#2
Do this instead...
10/21/2006 (10:50 am)
Hi,Do this instead...
struct JMTorqueData
: public GameBaseData
{
typedef GameBaseData Parent;
public:
DECLARE_CONOBJECT(JMTorqueData);
JMTorqueData();
~JMTorqueData();
static void consoleInit();
static void initPersistFields();
bool preload(bool server, char errorBuffer[256]);
virtual void packData(BitStream* stream);
virtual void unpackData(BitStream* stream);
JML_CHAR *siteswap; // StringTableEntry didn't make a difference.
JML_CHAR *pattern_name;
JML_CHAR *style;
GameBaseData * mBallData;
S32 bdataid; // ID of a datablock
};
JMTorqueData::JMTorqueData()
{
siteswap = dStrdup( "3");
pattern_name = dStrdup( "3 Balls");
style = dStrdup( "Normal");
mBallData = 0;
bdataid = 0;
}
JMTorqueData::~JMTorqueData()
{
if ( siteswap )
dFree( siteswap);
if ( pattern_name )
dFree( pattern_name);
if ( style )
dFree( style);
}
void JMTorqueData::initPersistFields()
{
Parent::initPersistFields();
addField("siteswap", TypeString, Offset(siteswap, JMTorqueData));
addField("pattern_name", TypeString, Offset(pattern_name, JMTorqueData));
addField("style", TypeString, Offset(style, JMTorqueData));
addField("ball_data", TypeGameBaseDataPtr, Offset(mBallData, JMTorqueData));
}
bool JMTorqueData::preload(bool server, char errorBuffer[256])
{
return Parent::preload(server, errorBuffer);
if ( ! mBallData && bdataid != 0 )
if ( ! Sim::findObject( bdataid , mBallData) )
Con::errorf( ConsoleLogEntry::General, "JMTorqueData::preload - Invalid packet, bad datablockId( mBallData): 0x%x", bdataid);
}
void JMTorqueData::packData(BitStream* stream)
{
Parent::packData(stream);
stream->writeString(siteswap,dStrlen(siteswap));
stream->writeString(pattern_name,dStrlen(pattern_name));
stream->writeString(style,dStrlen(style));
if ( stream->writeFlag( mBallData) )
stream->writeRangedU32( mBallData->getId(), DataBlockObjectIdFirst, DataBlockObjectIdLast);
}
void JMTorqueData::unpackData(BitStream* stream)
{
char buf[256];
Parent::unpackData(stream);
stream->readString(buf);
siteswap = dStrdup(buf);
stream->readString(buf);
pattern_name = dStrdup(buf);
stream->readString(buf);
style = dStrdup(buf);
if ( stream->readFlag() )
bdataid = (S32) stream->readRangedU32( DataBlockObjectIdFirst, DataBlockObjectIdLast);
}
datablock JMTorqueData(three)
{
ball_data = jmbd;
siteswap = "3";
style = "Normal";
pattern_name = "Three Balls";
};
Torque 3D Owner Drew Parker
Don't know if these will help your problem but they may be worth trying.
For doing the strings with the datablock:
.h file
Packing and unpack functions:
In terms of your three datablock, you have this line:
I'm pretty sure you can't assign realtime values to datablocks, since they are static and are passed over the net during mission load, also maybe before your object is created. Try just using static values and see if it works.
If you are trying to store a reference to your JugglingBallData datablock in the JMTorqueData, try using a name reference instead that is constant.