Hello,
I am trying to find a solution to save a FSM in order to restore it later (save game). I use Playmaker to sequence my game which is particularly narrative.
For now I can save local and global variables and get the current active state of an FSM and restore it via the SetState function.
But I also need to restart the last active action of this state or possibly to start the next unstarted action.
So I can retrieve information from each action of a state via the Active, Finished, Entered properties, but I can't set successfully a state directly on the wanted action. Indeed, SetState seems to partially reinitialize all the actions and to call OnEnter() on each of them
Here is an example of what I could code:
Save FSM function (simplified)
public override void Write(object obj, ES2Writer writer)
{
PlayMakerFSM data = (PlayMakerFSM)obj;
try
{
FsmState activeState = data.Fsm.ActiveState;
writer.Write( data.FsmVariables );
if ( activeState != null && activeState.ActiveAction != null )
writer.Write( data.Fsm.ActiveStateName );
else
writer.Write( string.Empty );
if ( !string.IsNullOrEmpty( data.Fsm.ActiveStateName ) )
{
bool isFinished = activeState.ActiveActions.Count == 0;
writer.Write( isFinished );
if ( !isFinished )
{
activeState.Actions.ToList().ForEach( act =>
{
writer.Write( act.Finished );
writer.Write( act.Active );
writer.Write( act.Entered );
} );
}
}
}
catch ( Exception e )
{
Debug.LogError( e.Message );
}
}
Load FSM function (simplified)
public override void Read( ES2Reader reader, object c )
{
PlayMakerFSM data = (PlayMakerFSM)c;
reader.Read<FsmVariables>( data.Fsm.Variables );
string stateName = reader.Read<string>();
if ( !string.IsNullOrEmpty( stateName ) )
{
bool isFinished = reader.Read<bool>();
if ( !isFinished )
{
data.Fsm.ActiveState.Actions.ToList().ForEach( act =>
{
act.Finished = reader.Read<bool>();
act.Active = reader.Read<bool>();
act.Entered = reader.Read<bool>();
} );
}
data.Fsm.SetState( stateName );
}
}
Do you have a proper solution for this issue or can you add, in the next Playmaker version, a feature to address it? (ex : SetState( stateName, actionIndex ) )
I know this is not enough to perform a full FSM snapshot but it would bring some flexibility required to meet my needs and the needs of some users.
Thank you very much for your answer.