🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

Declare string array in struct - How??

Started by
1 comment, last by WitchLord 18 years, 6 months ago
Hi I have this structure:

// C code
typedef struct
{
        int one;
        int two;
        string name[MAX_STRING_SIZE];
} _dbRecord;

extern _dbRecord dbRecord;
Which I am importing to the script like so: (error checking removed)

// C code
        result = scriptEngine->RegisterObjectType("_dbRecord", sizeof(_dbRecord), asOBJ_CLASS);
        result = scriptEngine->RegisterObjectProperty("_dbRecord", "int one", offsetof(_dbRecord,one));
        result = scriptEngine->RegisterObjectProperty("_dbRecord", "int two", offsetof(_dbRecord,two));
        //
        // Char type
        result = scriptEngine->RegisterObjectProperty("_dbRecord", "string name[256]", offsetof(_dbRecord,name));
But no matter what I do - I always error out:

16:50:10 > Script: Info: RegisterObjectProperty succeeded.
16:50:10 > Script: Error: Failed to RegisterObjectProperty.
16:50:10 > Script; Error: [ asINVALID_DECLARATION ]
16:50:10 > Couldn't start the script engine. Exiting.
What's the syntax I need to use to pass a 'string' inside a struct to the script? Thanks
Advertisement
It's not a string, it's an array.
Unfortunately you will not be able to register the name property as an array with AngelScript. AngelScript's arrays are not the same as C++ arrays. AngelScript's arrays are objects that allow you to resize the arrays and use bounds-checking when accessing the elements.

Perhaps you could register an object type that will represent the name property. But that would be a bit awkward, I think.

Instead I recommend that you register a couple of methods, getName() and setName(), for the structure. These methods could be implemented as global functions so that you don't have to modify the structure.

asCScriptString *getName(_dbRecord *o){  return new asCScriptString(o->name);}void setName(asCScriptString *s, _dbRecord *o){  strncpy(o->name, s->buffer.c_str(), MAX_STRING_SIZE);}engine->RegisterObjectMethod("_dbRecord", "string @getName()", asFUNCTION(getName), asCALL_CDECL_OBJLAST);engine->RegisterObjectMethod("_dbRecord", "void setName(const string ∈)", asFUNCTION(setName), asCALL_CDECL_OBJLAST);


Regards,
Andreas

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement