Sign in to follow this  
__ROB_D__

Passing Object by Ref/Pointer

Recommended Posts

Hi, I'm reckon this question must be asked all the time! But I'm having trouble getting AS to do what i'd like it to do. What I've got on My code side my game object class and my instances of the object
// C++
class GObject_c
{
int DataA;
float DataB;
void Update();
}

GObject_c Monsters[10];

Now I don't want AS to create instances of the object class I'd like to be able to pass by reference or pointer (Is this possible?) My goal is to be able to write AS the will update the GObject i.e.
// Angle Script
void UpdateMonster(GObject_c @Monster)
{
DataA = 12;
}

So what would be helpful is a simple example of how to do this? Thanks

Share this post


Link to post
Share on other sites
I believe you want this: Register an uninstanciable reference object

Basically you need to do this:


engine->RegisterObjectType("GObject", 0, asOBJ_REF);
engine->RegisterObjectBehaviour("GObject", asBEHAVE_ADDREF, "void f()", asMETHOD(GObject, AddRef), asCALL_THISCALL);
engine->RegisterObjectBehaviour("GObject", asBEHAVE_RELEASE, "void f()", asMETHOD(GObject, Release), asCALL_THISCALL);


By not registering the factory method AngelScript won't be able to instanciate any objects of the type.

If you don't want to implement reference counting for your GObject type (recommended, at least for debugging) then you can register the AddRef and Release behaviours using a dummy function that doesn't do anything.


void DummyFunc(void *) {}
engine->RegisterObjectBehaviour("GObject", asBEHAVE_ADDREF, "void f()", asFUNCTION(DummyFunc), asCALL_CDECL_OBJLAST);
engine->RegisterObjectBehaviour("GObject", asBEHAVE_RELEASE, "void f()", asFUNCTION(DummyFunc), asCALL_CDECL_OBJLAST);


Of course, then you won't know how many references AngelScript holds to your object.

Regards,
Andreas

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this