🎉 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!

member arrays

Started by
2 comments, last by WitchLord 19 years, 2 months ago
I have a bounding box struct which holds an array of 3 floats for its rotation around x, y and z. I've registered the struct and the array with AS, but whenever I try to use it AS always crashes (access vilation on line 206 of as_arrayobject.cpp, buff is always NULL). As its just a primitive array I thought it'd just work, but it doesn't seem to be. So what am I missing :D


// struct
typedef struct BBox
{
   float rot[3];
} BBox;


// register the bb struct with AS
r = asEng->RegisterObjectType("BBox", sizeof(BBox), asOBJ_CLASS_CA); assert(r >= 0);

// constructors etc
r = asEng->RegisterObjectBehaviour("BBox", asBEHAVE_CONSTRUCT, "void f()",             asFUNCTIONPR(BBoxCon, (BBox*), void), asCALL_CDECL_OBJLAST); assert(r >= 0);
r = asEng->RegisterObjectBehaviour("BBox", asBEHAVE_ASSIGNMENT, "BBox &f(BBox &in)",   asFUNCTION(BBoxAssign), asCALL_CDECL_OBJLAST); assert(r >= 0);

// register the float array
r = asEng->RegisterObjectProperty("BBox", "float[] rot(3)", offsetof(BBox, rot)); assert(r >= 0);



Registers fine, scripts that use it compile fine but it always crashes whenever rot gets used. I can't find anything special i need todo in the docs for primitive arrays so any ideas?
Advertisement
The problem is that arrays in AngelScript are not compatible with normal C++ arrays. If you register the member as an array, AngelScript assumes the array is the same structure registered for that type. In case of the default array type it uses the built-in asCArrayObject.

Also when registering the array, you are specifying the size of the array in the type declaration. AngelScript doesn't understand this, and will simply ignore it. (Maybe it should have given an error instead.)

If you need to register the member so that it can be accessed with the indexing operator, I suggest you register a simple type that exposes the index operator, and then register the array as that type. For example:

// A function for accessing the array with bounds checkingfloat *vector3f_index(asUINT i, float *a){  if( i >= 3 )  {    asIScriptContext *ctx = asGetActiveContext();    if( ctx ) ctx->SetException("Out of range");    return 0;  }  return &a;}// Register the fixed size array typeengine->RegisterObjectType("vector3f", 3*sizeof(float), asOBJ_CLASS);engine->RegisterObjectBehaviour("vector3f", asBEHAVE_INDEX, "float &f(uint)", asFUNCTION(vector3f_index), asCALL_CDECL_OBJLAST);engine->RegisterObjectBehaviour("vector3f", asBEHAVE_INDEX, "const float &f(uint) const", asFUNCTION(vector3f_index), asCALL_CDECL_OBJLAST);// Register the BBox with the vector3f as member...engine->RegisterObjectProperty("BBox", "vector3f rot", offsetof(BBox, rot)); 


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

ahh thanks heaps for the quick reply - looks simple enough. This should be one of my last calls for help as almost everything I want has been implimented :D Another week or two and I'll stop bothering you hehe
Don't stop! With your and other people's questions I learn what needs to be improved. So please continue giving me feedback on the library. [smile]

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