playMaker

Author Topic: Save active state and active action  (Read 1851 times)

XeNoTimE

  • Playmaker Newbie
  • *
  • Posts: 1
Save active state and active action
« on: February 07, 2017, 12:09:14 PM »
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)
Code: [Select]
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)
Code: [Select]
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.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15620
  • Official Playmaker Support
Re: Save active state and active action
« Reply #1 on: February 08, 2017, 01:43:12 AM »
Hi,

 you can save and activate a state but not an action, actions will be proceed as usual when you activate a state

 If you need granular control of actions, then you should have one action per state and then you only care of the active state to save and load.

There is no plan on giving granular control for action execution control I think.

 Bye,

 Jean