Jump to content
  • Advertisement
Sign in to follow this  
Davaris1

Constructors that take parameters?

This topic is 2639 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

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. :)

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!