playMaker

Author Topic: Go To Any State action  (Read 5823 times)

ClaudioFreda

  • Playmaker Newbie
  • *
  • Posts: 9
Go To Any State action
« on: February 19, 2014, 08:09:05 PM »
First of all, thanks for this great tool. My team is finding it ever-useful and I finally can talk game logic with the designers!

I'm having the need to go to a state by name.

Basically it's the same as the "Go to previous" action, but I need to store the state name in a variable and then switch it later.

This is because I have multiple global transitions going on at the same time, with states that last more than one frame, and I need to record the state where each of the global transitions was sent.

It would be very simple for me to code, but unfortunately the Fsm.SwitchState method is set to private.

This is what the action would look like if the method was public (does not work in current Playmaker):

Code: [Select]
using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Immediately switch to a state with the selected name.")]
public class GotoStateByName : FsmStateAction
{

[RequiredField]
FsmString stateName;

public override void Reset()
{
stateName = null;
}

public override void OnEnter()
{
Log("Goto State: " + stateName.Value);

Fsm.SwitchState(stateName.Value);

Finish();
}
}
}

Just changing this SwitchState method to public in the next update would help.

(in the meantime I'm using and ugly workaround but having the real thing would be awesome)
« Last Edit: February 19, 2014, 08:32:28 PM by ClaudioFreda »

ClaudioFreda

  • Playmaker Newbie
  • *
  • Posts: 9
Re: Go To Any State action
« Reply #1 on: February 20, 2014, 03:34:39 AM »
Ok, I was wrong on one thing.

SwitchState accepts a FsmState parameter, not a String.

Anyway, I was able to access the private method by using reflection. This is the (working in current Playmaker, not tested thoroughly) action.

It would be safer, of course, if SwitchState was public.

Code: [Select]
using UnityEngine;
using System;
using System.Reflection;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Immediately switch to a state with the selected name.")]
public class GotoStateByName : FsmStateAction
{

[RequiredField]
public FsmString stateName;

public override void Reset()
{
stateName = null;
}

public override void OnEnter()
{

FsmState targetState = null;
foreach (FsmState state in Fsm.States)
{
if (state.Name == stateName.Value)
{
targetState = state;
break;
}
}


if (targetState != null)
{
MethodInfo switchState = Fsm.GetType().GetMethod("SwitchState", BindingFlags.NonPublic | BindingFlags.Instance);
Log("Goto State: " + stateName.Value);
switchState.Invoke(Fsm, new object[] { targetState });
}
else LogError("State Switch: State does not exist.");

Finish();
}
}
}
« Last Edit: February 20, 2014, 03:44:11 AM by ClaudioFreda »

Tibo

  • Playmaker Newbie
  • *
  • Posts: 5
Re: Go To Any State action
« Reply #2 on: November 01, 2014, 04:32:19 PM »
Thank you for this action.
I am using also global events for transitionning, as I find it more readable and this action is exactly what I needed.

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Go To Any State action
« Reply #3 on: November 11, 2014, 02:15:52 AM »
Hi,

 ok, I reworked this action a bit and it's now available on the ecosystem, you can download it directly as well here.

 This action will now allow you to target a different fsm on a different gamobject, as well as sending event if state was found or not.

Bye,

 Jean