This guide will show you how you can declare a generic callback thanks to Codeware's reflection.
Requirements
You need Codeware (Nexus | Wiki) to use the following classes. It provides a Reflection module which is exactly what we need to make our callback in a generic way.
How to
We will declare two classes: Callback to use with methods of a class, and StaticCallback to use with static methods of a class.
We declare Callback which inherits DelayCallback. This way we can use Callback if we need it as-is, and we can also use it with DelaySystem.
We implement the method Call() which is expected by DelaySystem / DelayCallback. If we want to use it without DelaySystem, we simply need to call Call() when we want to execute a callback by ourself.
Example
publicclassMyService extends ScriptableService {// ...public cb funcOnSessionReady(event: ref<GameSessionEvent>) {if event.IsPreGame() {return; }let delaySystem = GameInstance.GetDelaySystem(GetGameInstance());let callback = Callback.Create(this, n"OnPlay", [42]);let delay: Float=1.0; // seconds delaySystem.DelayCallback(callback, delay, false);// callback.Call(); }// [cb] is required to make function a callback.// [arg] is an optional argument which can be passed when calling [Callback.Create].private cb funcOnPlay(arg: Int32) {FTLog(s"arg: \(arg)");// do stuff after [delay] s. }}
This looks like Callback, we still inherits DelayCallback to be compatible with DelaySystem. But this time we only need a function's name.
Static methods of a class are registered as global functions. You'll need to use this syntax: n"ModuleName.ClassName::MethodName". Method must be declared with keyword cb. ModuleName is optional.
Example
This shows how it looks like when declaring in a custom module MyModule. It is not mandatory and you can declare it in the global scope.
module MyModulepublicclassMyService extends ScriptableService {// ...public cb funcOnSessionReady(event: ref<GameSessionEvent>) {if event.IsPreGame() {return; }let delaySystem = GameInstance.GetDelaySystem(GetGameInstance());let callback = StaticCallback.Create(n"MyModule.MyService::OnPlay", [n"Choom"]);let delay: Float=1.0; // seconds delaySystem.DelayCallback(callback, delay, false);// callback.Call(); }// [cb] is required to make function a callback.// [name] is an optional argument which can be passed when calling [StaticCallback.Create].privatestatic cb funcOnPlay(name: CName) {FTLog(s"name: \(name)");// do stuff after [delay] s. }}