arrays in script
by Stefan Beffy Moises · in Torque Game Engine · 07/02/2002 (12:33 pm) · 7 replies
Hi there,
got a little problem writing my TGEConEd classes...
I got new class tgeConEd, which, among various other things, defines a datablock similar to this one here:
the object is instantiated correctly, I can access, e.g. TConLabel from the console, e.g. via "echo(DefaultSequence.TConLabel);" - but how do the arrays work then?
I only get the first "element", but not as I would expect with "DefaultSequence.TConText[0]", but with
"DefaultSequence.TConText" (which then returns
"Bla bla bla bla" in this case)... DefaultSequence.TConText[0] and DefaultSequence.TConText[1] don't return anything...
If I use my C++ methods for setting/getting the array elements, everything works fine, here are some snippits:
Thanks a lot! :-)
got a little problem writing my TGEConEd classes...
I got new class tgeConEd, which, among various other things, defines a datablock similar to this one here:
new TGEConSequence(DefaultSequence)
{
TConID = "1";
TConLabel = "seq00";
TConParticipant1 = "Bot0";
TConParticipant2 = "[ToRK]beffy";
TConText[0] = "Bla bla bla bla";
TConAudioFile[0] = "~/data/conversations/audio/mission1/mrx001.wav";
TConText[1] = "Hey there!";
TConAudioFile[1] = "~/data/conversations/audio/mission1/mrx001.wav";
};Now the problem are the array type things you see there, e.g. TConText[0] and TConText[1]...the object is instantiated correctly, I can access, e.g. TConLabel from the console, e.g. via "echo(DefaultSequence.TConLabel);" - but how do the arrays work then?
I only get the first "element", but not as I would expect with "DefaultSequence.TConText[0]", but with
"DefaultSequence.TConText" (which then returns
"Bla bla bla bla" in this case)... DefaultSequence.TConText[0] and DefaultSequence.TConText[1] don't return anything...
If I use my C++ methods for setting/getting the array elements, everything works fine, here are some snippits:
void TGEConSequence::setTConText(const char* Text, U32 index){
mTConText[index] = StringTable->insert(Text);
}
const char* TGEConSequence::getTConText(U32 index){
return mTConText[index];
}But how do I get them filled from the datablock and how do I access them directly from script?? Thanks a lot! :-)
About the author
#2
I found TypeEnum, but if I use it instead, it crashes the engine... hmmmmm....
07/02/2002 (12:46 pm)
Hm, guess part of the problem is void TGEConSequence::initPersistFields()
{
...
addField("TConText", TypeString, Offset(mTConText, TGEConSequence));but how do I expose the my array to the scripting engine correctly...?I found TypeEnum, but if I use it instead, it crashes the engine... hmmmmm....
#3
so here is all the code related to this text array... maybe I got an error somewhere and I'm too blind to see it...
07/02/2002 (1:00 pm)
Okay,so here is all the code related to this text array... maybe I got an error somewhere and I'm too blind to see it...
const char* mTConText[MaxSequences];
void setTConText(const char* Text, U32 index);
const char* getTConText(U32 index);
void TGEConSequence::setTConText(const char* Text, U32 index){
mTConText[index] = StringTable->insert(Text);
}
const char* TGEConSequence::getTConText(U32 index){
return mTConText[index];
}
static void csetTConText(SimObject * obj, S32, const char ** argv)
{
TGEConSequence *seq = static_cast<TGEConSequence*>(obj);
if (!seq) return;
seq->setTConText(argv[2], dAtoi(argv[3]));
}
static const char * cgetTConText(SimObject *obj, S32, const char** argv)
{
TGEConSequence *seq = static_cast<TGEConSequence*>(obj);
const char* text = seq->getTConText(dAtoi(argv[2]));
return(text);
}
void TGEConSequence::initPersistFields()
{
Parent::initPersistFields();
addField("TConText", TypeString, Offset(mTConText, TGEConSequence));
}
void TGEConSequence::consoleInit()
{
Con::addCommand("TGEConSequence", "setTConText", csetTConText,
"tConSeq.setTConText(text, index)", 4, 4);
Con::addCommand("TGEConSequence", "getTConText", cgetTConText,
"tConSeq.getTConText(index)", 3, 3);
}Hm, that's pretty much it concerning this text array ... anybody found any errors?
#4
Anyways, that's not what you want.
This is:
07/02/2002 (1:09 pm)
TypeEnum requires a static array.Anyways, that's not what you want.
This is:
// Instead of this:
void TGEConSequence::initPersistFields()
{
...
addField("TConText", TypeString, Offset(mTConText, TGEConSequence));// Put this:
void TGEConSequence::initPersistFields()
{
...
addField("TConText", TypeString, Offset(mTConText, TGEConSequence),iNumElements);
// Here, iNumElements is the number of elements in the mTConText array
#5
Is this appended number telling the scripting engine that this "TypeString" is actually an array?
Anyhow, now it works! :-) Back to work...
And thanks again for the quick help!!
07/02/2002 (1:23 pm)
Hey, thanks a lot - that did it!!! But why?Is this appended number telling the scripting engine that this "TypeString" is actually an array?
Anyhow, now it works! :-) Back to work...
And thanks again for the quick help!!
#6
07/02/2002 (2:01 pm)
Little addon... ;) Is it possible to get the length of the current array in script? like "TConText.length()" or "TConText.size()" or something? Or do I have to use my own counter for this?
#7
I know this thread has been dead for a very long time but it's the closest topic I found to my question so I thought I'd try following up here. Can you use a Vector (defined in tVector.h) similarly to how you can use an array? Also, do arrays have to be static? In the example above, is iNumElements static or can the array grow? Anyway, back to my Vector based question...
I've created a struct called VEEQuad which takes 4 VEE_Vector3 types to create a plane. For this discussion, consider VEE_Vector3 to be a Point3F. So my struct is...
Then I'm defining my ConsoleType...
Then I'm setting up my vector...
And finally was trying to use it in my initPersistFields...
I am getting the following error: invalid use of member 'VEECollision::mQuadList' in static member function
So, I could have any number of mistakes in here (and welcome anyone who finds them to point them out) but if this isn't even doable, I'd like to know that too.
Thanks,
Ben
03/14/2007 (8:20 pm)
Hi all,I know this thread has been dead for a very long time but it's the closest topic I found to my question so I thought I'd try following up here. Can you use a Vector (defined in tVector.h) similarly to how you can use an array? Also, do arrays have to be static? In the example above, is iNumElements static or can the array grow? Anyway, back to my Vector based question...
I've created a struct called VEEQuad which takes 4 VEE_Vector3 types to create a plane. For this discussion, consider VEE_Vector3 to be a Point3F. So my struct is...
struct VEEQuad { VEE_Vector3 a,b,c,d; };Then I'm defining my ConsoleType...
DefineConsoleType( TypeVEEQuad )
ConsoleType( VEEQuad, TypeVEEQuad, sizeof(VEEQuad) )
ConsoleGetType( TypeVEEQuad )
{
VEEQuad *qd = (VEEQuad *) dptr;
char* returnBuffer = Con::getReturnBuffer(256);
dSprintf(returnBuffer, 256, "{ \"%g %g %g\", \"%g %g %g\", \"%g %g %g\", \"%g %g %g\" }", qd->a.x, qd->a.y, qd->a.z, qd->b.x, qd->b.y, qd->b.z, qd->c.x, qd->c.y, qd->c.z, qd->d.x, qd->d.y, qd->d.z);
return returnBuffer;
}
ConsoleSetType( TypeVEEQuad )
{
if(argc == 4)
{
dSscanf(argv[0], "%g %g %g", &((VEEQuad *) dptr)->a.x, &((VEEQuad *) dptr)->a.y, &((VEEQuad *) dptr)->a.z);
dSscanf(argv[1], "%g %g %g", &((VEEQuad *) dptr)->b.x, &((VEEQuad *) dptr)->b.y, &((VEEQuad *) dptr)->b.z);
dSscanf(argv[2], "%g %g %g", &((VEEQuad *) dptr)->c.x, &((VEEQuad *) dptr)->c.y, &((VEEQuad *) dptr)->c.z);
dSscanf(argv[3], "%g %g %g", &((VEEQuad *) dptr)->d.x, &((VEEQuad *) dptr)->d.y, &((VEEQuad *) dptr)->d.z);
}
else
Con::printf("VEEQuad must be set as { \"x y z\", \"x y z\", \"x y z\", \"x y z\" }");
}Then I'm setting up my vector...
Vector<VEEQuad*> mQuadList;
And finally was trying to use it in my initPersistFields...
addField("QuadList", TypeVEEQuad, Offset(mQuadList, VEECollision), mQuadList.size);I am getting the following error: invalid use of member 'VEECollision::mQuadList' in static member function
So, I could have any number of mistakes in here (and welcome anyone who finds them to point them out) but if this isn't even doable, I'd like to know that too.
Thanks,
Ben
Torque 3D Owner Robert Blanchet Jr.
Alternatively I think this works as well(not sure though):
I think both ways work. So are you absolutely sure your code is correct?