Engine code question about StringTableEntry.
by Kevin Mitchell · in Torque 3D Professional · 11/27/2011 (1:50 pm) · 2 replies
So I've been going crazy with trying to get my data into my custom objects and i've stumbled on to something that confuses me. When looking at how other places use StringTableEntry i do not see them passing that string into the StringTable->insert() function. If i try this my stored buffer is corrupted on the next function call. I see that StringTableEntry is just a char * referenced which leads me to believe that when using this type on a function param for DefineEngineMethod this would create a new buffer for the string buffer. But if i call that same function it looks like the pointer is reused for all consective calls to the function. So I had to use StringTable->insert() and pass the function buffer string into that. But my question is that why do i not see this anywhere else in the engine. Is it something I'm doing?
Heres my Defines for the Method.
So unless I'm blind (which i may be) I don't see where there maybe an issue where it uses the same buffer for strings on the function call unless its something in the DefineEngineMethod definition.
void RPGParty::addPlayerInfo(
S32 index,
StringTableEntry Name,
StringTableEntry Race,
S32 LEVEL,
S32 ClassID,
StringTableEntry ClassName,
StringTableEntry Bio){
if(PartyMemberCount<=index)return;
//NOT WORKING :/
//PartyMembers[index].Name=Name;
//PartyMembers[index].Race=Race;
//PartyMembers[index].ClassName=ClassName;
//PartyMembers[index].Biography=Bio;
PartyMembers[index].Name=StringTable->insert(Name);
PartyMembers[index].Race=StringTable->insert(Race);;
PartyMembers[index].Level=LEVEL;
PartyMembers[index].ClassID=ClassID;
PartyMembers[index].ClassName=StringTable->insert(ClassName);
PartyMembers[index].Biography=StringTable->insert(Bio);
}Heres my Defines for the Method.
DefineEngineMethod( RPGParty, addPlayerInfo, void, (S32 INDEX,
StringTableEntry Name,
StringTableEntry Race,
S32 LEVEL,
S32 ClassID,
StringTableEntry ClassName,
StringTableEntry Bio),,
"addPlayerInfo(INDEX,Name,Race,LEVEL,ClassID,ClassName,Bio)")
{
object->addPlayerInfo( INDEX,Name,Race,LEVEL,ClassID,ClassName,Bio);
}So unless I'm blind (which i may be) I don't see where there maybe an issue where it uses the same buffer for strings on the function call unless its something in the DefineEngineMethod definition.
About the author
Riding Solo since 2005. Current Project: Fated World 2005-Present RPG Engine Tool Kit - Now available.
#2
11/28/2011 (11:53 am)
Thanks for that Info that helped a lot. Will be making changes when i get home.
Associate Fyodor -bank- Osokin
Dedicated Logic
void RPGParty::addPlayerInfo( S32 index, StringTableEntry Name, StringTableEntry Race, S32 LEVEL, S32 ClassID, StringTableEntry ClassName, StringTableEntry Bio){ if(PartyMemberCount<=index)return; PartyMembers[index].Name=Name; PartyMembers[index].Race=Race; PartyMembers[index].ClassName=ClassName; PartyMembers[index].Biography=Bio; PartyMembers[index].Level=LEVEL; PartyMembers[index].ClassID=ClassID; } DefineEngineMethod( RPGParty, addPlayerInfo, void, (S32 INDEX, const char * Name, const char * Race, S32 LEVEL, S32 ClassID, const char * ClassName, const char * Bio),, "addPlayerInfo(INDEX,Name,Race,LEVEL,ClassID,ClassName,Bio)") { object->addPlayerInfo( INDEX, StringTable->insert(Name), StringTable->insert(Race), LEVEL, ClassID, StringTable->insert(ClassName), StringTable->insert(Bio)); }You can't use StringTableEntry in the way you have put it in DefineEngineMethod, as engine will simply use const char * to pass the data (without storing it in StringTable).In my example, we are using "read" const char * to get the data from console sub-system and passing it to engine as StringTableEntry (by inserting it to StringTable).
No crashes, everything should work fine.
Or, you can do this way:
void RPGParty::addPlayerInfo( S32 index, const char * Name, const char * Race, S32 LEVEL, S32 ClassID, const char * ClassName, const char * Bio){ if(PartyMemberCount<=index)return; PartyMembers[index].Name=StringTable->insert(Name); PartyMembers[index].Race=StringTable->insert(Race);; PartyMembers[index].Level=LEVEL; PartyMembers[index].ClassID=ClassID; PartyMembers[index].ClassName=StringTable->insert(ClassName); PartyMembers[index].Biography=StringTable->insert(Bio); } DefineEngineMethod( RPGParty, addPlayerInfo, void, (S32 INDEX, const char * Name, const char * Race, S32 LEVEL, S32 ClassID, const char * ClassName, const char * Bio),, "addPlayerInfo(INDEX,Name,Race,LEVEL,ClassID,ClassName,Bio)") { object->addPlayerInfo( INDEX, Name, Race, LEVEL, ClassID, ClassName, Bio); }Again, you can't use StringTableEntry in consoleMethods.Yes, StringTableEntry is "const char *" but it points to the storage space inside StringTable and it doesn't auto-adds it to StringTable when used in console methods/functions, so you need to do it by yourself.
test1 and ste1 will differs, as one points right to the string and second points to the copy of it, but inside the StringTable. So, next time you call StringTable->insert("This is test string") it will return same address as ste1, as the exact string is found and already exists in table.