Sign in to follow this  
huangyous

class method as Event Handler

Recommended Posts

huangyous    122
I want to register AS class method as an event handler like OnClick ,here is an example in AS: class Dialog { Dialog() { //register OnClick as event handler } void OnClick() {...} UI@ ui; }; Dialog dlg1=Dialog(); in C++ class UI { void* pOnClick_Caller; int OnClick_FuncId; void Click() { ctx->Prepare(OnClick_FuncId); ctx->SetObject(pOnClick_Caller); ctx->Execute(); } }; So in Dialog() { } can I write some code like ui.RegisterCallback(this,Dialog::OnClick)? or write something like i.Register("modulename","Dialog",this,"void Onlick()")? Can anybody please give me an sample code?

Share this post


Link to post
Share on other sites
WitchLord    4677
Until I implement function pointers in AngelScript you will have to do it by naming the function, e.g.


// C++ function, registered as
// "void RegisterEventHandler(IEventHandler @, const string &in)"
void RegisterEventHandler(asIScriptObject *obj, const string &name)
{
// Find the method on the object
asIObjectType *type = obj->GetObjectType();
int funcId = type->GetMethodIdByName(name.c_str());

// Store the obj and funcId for later callback
...
}



// AngelScript class
class Dialog : IEventHandler
{
Dialog() { RegisterEventHandler(this, "OnClick"); }
void OnClick() {}
}


By using an interface like this, your function gets a little easier to implement. But if you don't like to use the interface, then you can also implement the RegisterEventHandler using the variable parameter type.

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