Jump to content
  • Advertisement
Sign in to follow this  
huangyous

class method as Event Handler

This topic is 3340 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 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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

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

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!