Hi everyone!
New Playmaker user here. So please bear with me
I'm familiar enough with Unity and C# scripting, but very new to Playmaker. I'm creating custom actions for some of my existing scripts. Everything went fairly well, until I hit a certain method that receives an asynchronous callback.
Say I have this fictitious method
UserData.RetrieveAge(string username, System.Action<int> callback)
which retrieves the user's age from a remote server, with the callback being invoked once the server responds. How should I design my action to properly handle this? The first thing came to my mind is something like this
public FsmString username;
public FsmInt userAge;
public FsmEvent callbackEvent;
public override void OnEnter()
{
UserData.RetrieveAge(username.Value, MyCallback);
Finish();
}
void MyCallback(int age)
{
userAge.Value = age;
Fsm.Event(callbackEvent);
}
Is this a proper design? I'm afraid not, thinking of 2 scenarios:
1. Since the action finishes right after the call to UserData.RetrieveAge, if I make a transition upon FINISHED and move to a next state, then MyCallback will never be invoked because the state became inactive (am I right here?).
2. If I remove the Finish() call, the FSM will stay at the current state waiting for MyCallback to be invoked and send the callbackEvent event, which may never happen due to network/server issues. In that case the FSM will get stuck at the current state forever (?)
To my understanding, the ideal approach would be to have the action finish right after the RetrieveAge call, and somehow have the callback send a global event/transition. The question is how to achieve that in Playmaker?
What is your common design pattern for methods like this? Sorry if I miss something obvious.
Thanks for reading!