Jump to content

[SOLVED] Callbacks?


photo

Recommended Posts

Posted

Hi,

 

I need to implement some callbacks into my scripts. By looking trough the documentation I found the GameEvent and GameCallback classes .. but unfortunately there is no real documentation or an example on how to use them.

 

 

As an example what I need:

class MyClass {

  void doSomething() {
    [...]
    runAllRegisteredCallbacks(myArg);
  }

  void registerCallback(callback) {
    registeredCallbacks.append(callback);
  }
}

MyClass cls;
cls.registerCallback(myFunction);
...

How would I do this in Unigine Script?

 

Cheers,

Daniela

Posted

Hi,

Have you checked <UNIGINE_DIR>/data/core/scripts/callback.h?

 

For this kind of subscription service I use a map (string : CallbackBase) creating the callback objects using the MakeCallback() and MakeCallbackObject() functions.

//...

//define container
Unigine::CallbackBase p_type_callbacks[]= (:); //registered type callbacks

//...

//register callbacks for different events
p_type_callbacks.append("login",	Unigine::MakeCallbackObject(this, "_processRequestLogIn"));
p_type_callbacks.append("ssid",			Unigine::MakeCallbackObject(this, "_processSSID"));

//...

//invoke callback
string myarg= "an argument for the callback";
if( p_type_callbacks.check(type) ) {	
	p_type_callbacks[type].run(my_arg);
}

×
×
  • Create New...