Game Development Community

Exposing an array of strings to the scripting engine..

by J. Donavan Stanley · in Torque Game Engine · 06/14/2003 (8:29 am) · 14 replies

Anyone know of a way to expose an array of string to the scripting engine?

I have a member variable declared like thus: "StringTableEntry mStrs[CONST_VAL]"

I tried to expose the individual elements as fields like this:

char buf[256];
for( int x=0; x < CONST_VAL; X++ )
{
   dSprintf( buf, 256, "str%d", x );
   addField( buf, TypeString, Offset( mStrs[x], MyObject));
}

But it blows up.

#1
06/14/2003 (1:53 pm)
That's a very Javaish way of exposing constants, if those _are_ constants.

There's support for arrays in the scripting engine; I'd suggest using that. Sorry, I don't have the code in front of me right now, otherwise I'd give you a code snippet. Mail me and I can dig something up for you Monday.
#2
06/14/2003 (2:57 pm)
They're not constants. They're actualy filenames. I'm going to have to dig some more in the console code. Closest thing I saw to array support was support for things like vector.

Right now I'm waiting for a complete import of Torque to go through over a 56k modem. Blech!
#3
06/15/2003 (8:02 am)
The only ways I've seen this sort of thing done is to space-seperate the strings and use getWord() in the script language to retrieve each "word" from the main string. However, this wouldn't work if your substrings have spaces in them.
#4
06/15/2003 (8:52 am)
Well crap. Maybe I'll use a DML or something then. All i'm wanting to do is allow TerrainManager to keep track of X number of Terrain Textures, Details and Bumpmaps without having to hardcode the names in case "X" changes later.
#5
06/15/2003 (12:04 pm)
Maybe all you need is an "addTerrainTexture(type, filepath)" function?
#6
06/15/2003 (12:09 pm)
That doesn't cover usage in datablocks. I wanted some way to specify the various elements of my array in a way that's compatible with getField. I've still no settle on just how I want to do this yet so it's getting back burnered.
#7
06/15/2003 (12:14 pm)
Correct me if im wrong,
but can you not make this look more like how it is done for sky.
and have it work fine?

it looks the same to me.
#8
06/15/2003 (12:49 pm)
Yeah I can use a DML, and that's what I'm currently leaning towards. My issue with that is you now have two files to deal with when setting up terrain details/bumps.
#9
06/16/2003 (11:36 am)
If you need to specify an array of filenames in the datablock, you can do just that.

detail[0] = "blah/blah1";
detail[1] = "blah/blah2";
detail[2] = "blah/blah3";
and so on. The c code can pick that up and use it. I've done it a couple of times for animation textures and the like. This works for anything really in the scripting engine. But, I could just be misinterpreting you...
#10
06/16/2003 (12:12 pm)
The question is how do I write the fields on the C++ side? i.e. What's the addField syntax to expose a specific element of an array of strings.
#11
06/16/2003 (12:24 pm)
I didnt mean the dml
have a gander at the Sky init method.
it is exposing variables from an array like you want.
#12
06/16/2003 (12:32 pm)
Ahhhhhhhhh Now I see the syntax. Thanks Badguy!

For those playing at home its:
addField( NAME, TYPE, Offset( VAR, OBJ), SIZE_OF_ARRAY);

That last bit was what I was missing.
#13
06/16/2003 (12:54 pm)
:)
groovy man glad I could help.
#14
06/16/2003 (1:33 pm)
Well, I rejoiced too soon. I can access them properly in script now, and if I edit the datablock by hand it's all good but the gui only displays the first string.

After looking at sky.cc some more it became clear that what I tried the first time should have worked. I had initialized the arrays via dMemset( arry, 0, MaterialGroups * sizeof(char*)). And whenever you tried to expand the "media" section you ended up crashing in expandEscape. I went back and changed it to loop though the arrays setting them to an empty string (like sky.cc does) and it started working.