Getting a DataBlock from C
by Steve Bilton · in Torque Game Engine · 01/10/2008 (8:57 am) · 10 replies
Apologies in advance if this is a stupid question, but I've been trawling the forums and docs but can't seem to find the answer to what I assume is a fairly simple question.
I'm trying to grab hold of fields within a datablock defined in scripts while in C++.
So my scripts have:
datablock PlayerData(DemoPlayer : PlayerBody)
{
someField = 10;
};
Then in C++, I have the following:
// Get my current player that I'm dealing with - this could well be very ugly.
SimObject* object = 0;
Sim::findObject(dAtoi(Con::getVariable("$AIManager::currentPathPlayer")), object);
player = (AIPlayer*)object;
if ( player )
{
GameBaseData dataBlock = player->getDataBlock();
const char* myField = datablock->getDataField(someField, NULL);
}
I'm getting myField as NULL, but it gets dataBlock okay. The question, then, is how I get my field from the specified datablock?
Cheers
I'm trying to grab hold of fields within a datablock defined in scripts while in C++.
So my scripts have:
datablock PlayerData(DemoPlayer : PlayerBody)
{
someField = 10;
};
Then in C++, I have the following:
// Get my current player that I'm dealing with - this could well be very ugly.
SimObject* object = 0;
Sim::findObject(dAtoi(Con::getVariable("$AIManager::currentPathPlayer")), object);
player = (AIPlayer*)object;
if ( player )
{
GameBaseData dataBlock = player->getDataBlock();
const char* myField = datablock->getDataField(someField, NULL);
}
I'm getting myField as NULL, but it gets dataBlock okay. The question, then, is how I get my field from the specified datablock?
Cheers
About the author
#2
01/10/2008 (10:00 am)
Also, and call me a terrible programmer if you want, you could consider making the field public, so:const char* myField = datablock->someField;
#3
As an aside on your second comment, I wasn't aware you could set a field to be public/private within the scripts ... as this is the only place myField was declared.
01/10/2008 (10:14 am)
Excellent, the string table grab worked, cheers. Out of interest, why was the string table necessary? Is there a link to some information at what is actually going on here?As an aside on your second comment, I wasn't aware you could set a field to be public/private within the scripts ... as this is the only place myField was declared.
#4
the StringTable's reason for existence is that it always returns the same const char* for the same input string, so that later you can just compare the const char*s directly with "==" instead of using strcmp().
.. which is what's going on in SimFieldDictionary::getFieldValue():
01/10/2008 (10:32 am)
Oh, sorry - i take back my second comment; i didn't read your post closely enough.the StringTable's reason for existence is that it always returns the same const char* for the same input string, so that later you can just compare the const char*s directly with "==" instead of using strcmp().
.. which is what's going on in SimFieldDictionary::getFieldValue():
const char *SimFieldDictionary::getFieldValue(StringTableEntry slotName)
{
U32 bucket = HashPointer(slotName) % HashTableSize;
for(Entry *walk = mHashTable[bucket];walk;walk = walk->next)
[b]if(walk->slotName == slotName)[/b]
return walk->value;
return NULL;
}
#5
01/10/2008 (10:37 am)
Ah right, I get you. Ta. =)
#6
01/10/2008 (10:47 am)
@Orion - I'd call you a terrible programmer, but the whole pot calling the kettle black thing would come into play =)
#7
but what should be as parament array when i want access to array value
when i access to "test[1,1]"
i try those 2 variants and nothing work
08/03/2009 (8:05 am)
this work wellconst char* myField = datablock->getDataField(StringTable->insert(someFieldName), NULL);
but what should be as parament array when i want access to array value
when i access to "test[1,1]"
i try those 2 variants and nothing work
const char* myField = datablock->getDataField(StringTable->insert("test"), "1,1");
const char* myField = datablock->getDataField(StringTable->insert("test"), "1_1");
#8
08/03/2009 (9:56 am)
i think there may be a special version of getDataField to deal w/ array'd fields.
#9
08/03/2009 (11:05 am)
const char* myField = datablock->getDataField(StringTable->insert("test"), "1_1");That is exactly how to do it. It wont work for static fields but you cannot expose multidimensional arrays to script through static fields anyway.
#10
08/03/2009 (9:58 pm)
You are right, this code work, i had typo in it...const char* myField = datablock->getDataField(StringTable->insert("test"), "1_1");
Associate Orion Elenzil
Real Life Plus