I wrote this to retrieve a variable from my script and store it in FSM variable. It seems to work like a charm, but i would like to know if there might be issues or if there is a better way to do it.
#if PLAYMAKER
using HutongGames.PlayMaker;
using HutongGames.PlayMakerEditor;
namespace MagicAI
{
[ActionCategory(ActionCategory.ScriptControl)]
[HutongGames.PlayMaker.Tooltip("Gets the GameObj from AI's Blackboard and sets it as variable in FSM. AI must be parent to this FSM")]
public class GetMagicAIGameObjFromBlackboard : FsmStateAction
{
[HutongGames.PlayMaker.Tooltip("Variable to set to variable from MagicAI Blackboard")]
//public FsmVariable variableToSet;
public FsmVar variableToSet;
public string keyInBlackboard;
private NamedVariable targetVariable;
public override void OnEnter()
{
base.OnEnter();
AI ai = FindAI();
if (ai)
{
Blackboard bb= ai.GetComponent<Blackboard>();
if (bb == null)
{
Debug.LogWarning("MagicAI: Play Maker: No Blackboard found! FSM is " + this.Fsm.Name);
}
else
{
GameObject go = bb.Get(keyInBlackboard);
if (go == null)
{
Debug.LogWarning("MagicAI: Play Maker: GameObj '" + keyInBlackboard + "'retrieved from to Blackboard is null! FSM is " + this.Fsm.Name);
}
else
{
targetVariable = Fsm.Variables.GetVariable(variableToSet.variableName);
variableToSet.Type = targetVariable.VariableType;
variableToSet.gameObjectValue = go;
variableToSet.ApplyValueTo(targetVariable);
}
}
}
Finish();
}
AI FindAI()
{
AI ai = this.Fsm.GameObject.GetComponentInParent<AI>();
if (ai) return ai;
Debug.LogWarning("MagicAI: Play Maker: No AI on parent found on " + this.Fsm.Name);
return null;
}
}
}
#else
namespace MagicAI
{
}
#endif