Constructors that take parameters?

Started by
0 comments, last by droz 12 years, 6 months ago
I want an object to behave like this in AngelScript, but am unable to get it to work.


// AngelScript Code:

ZedScriptW p(ptrToZedEntityID1);
ZedScriptW s(ptrToZedEntityID2);



This part seems to work.


// C++
r = engine->RegisterObjectType("ZedScriptW", sizeof(ZedScriptW), asOBJ_VALUE|asOBJ_APP_CLASS|asOBJ_APP_CLASS_CONSTRUCTOR|asOBJ_APP_CLASS_DESTRUCTOR); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT, "void f()", asFUNCTION(ZedScriptW_Constructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );

r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_DESTRUCT, "void f()", asFUNCTION(ZedScriptW_Destructor), asCALL_CDECL_OBJLAST); assert( r >= 0 );



However I can't figure out how to create a constructor that accepts multiple parameters.

This is my attempt


// C++

r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT, "void f(ZedEntityID @)", asFUNCTIONPR(ZedScriptW_Constructor_ZedEntityID, (ZedScriptW*, ZedEntityID*), void), asCALL_CDECL_OBJLAST); assert( r >= 0 );


This is the code for the constructor that takes a parameter. The function is called when I run it, but ownerID points to garbage.


// C++

void ZedScriptW_Constructor_ZedEntityID(ZedScriptW *memory, ZedEntityID *ownerID)
{
new(memory) ZedScriptW();

if (ownerID)
memory->SetComponent(ownerID->GetOwner());
}


Can anyone help?


Edit:
I think I have figured it out while writing this post. asCALL_CDECL_OBJLAST is the hint. So I have swapped the params around.


r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT, "void f(ZedEntityID @)", asFUNCTIONPR(ZedScriptW_Constructor_ZedEntityID, ( ZedEntityID*, ZedScriptW*), void), asCALL_CDECL_OBJLAST); assert( r >= 0


void ZedScriptW_Constructor_ZedEntityID(ZedEntityID *ownerID, ZedScriptW *memory)


Hopefully all of the above is correct. If not please let me know. :)
Advertisement

Edit:
I think I have figured it out while writing this post. asCALL_CDECL_OBJLAST is the hint. So I have swapped the params around.


r = engine->RegisterObjectBehaviour("ZedScriptW", asBEHAVE_CONSTRUCT, "void f(ZedEntityID @)", asFUNCTIONPR(ZedScriptW_Constructor_ZedEntityID, ( ZedEntityID*, ZedScriptW*), void), asCALL_CDECL_OBJLAST); assert( r >= 0


void ZedScriptW_Constructor_ZedEntityID(ZedEntityID *ownerID, ZedScriptW *memory)


Hopefully all of the above is correct. If not please let me know. :)


Yeah thats correct, alternatively you could of changed the calling convention to asCALL_CDECL_OBJFIRST.

This topic is closed to new replies.

Advertisement