Advertisement

class method as Event Handler

Started by May 03, 2009 12:08 AM
0 comments, last by WitchLord 15 years, 6 months ago
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?
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 ∈)"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 classclass 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

AngelCode.com - game development and more - Reference DB - game developer references
AngelScript - free scripting library - BMFont - free bitmap font generator - Tower - free puzzle game

This topic is closed to new replies.

Advertisement