playMaker

Author Topic: Add FSM Template at Runtime? [SOLVED]  (Read 19086 times)

JennaReanne

  • Junior Playmaker
  • **
  • Posts: 57
    • Little Worlds Interactive
Re: Add FSM Template at Runtime?
« Reply #15 on: June 19, 2014, 01:52:03 PM »
I know this thread is old but wow, these actions are amazing.  Thank you so much for sharing!

xunxun

  • Junior Playmaker
  • **
  • Posts: 53
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #16 on: September 25, 2014, 04:45:54 AM »
this is awesome. Thanks much!

ClintonReddie

  • Playmaker Newbie
  • *
  • Posts: 7
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #17 on: October 13, 2014, 01:57:22 AM »
More thanks.

joepalos

  • Playmaker Newbie
  • *
  • Posts: 28
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #18 on: July 31, 2016, 02:56:20 PM »
Thanks a lot for this, amazing.

MarkD

  • Full Member
  • ***
  • Posts: 113
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #19 on: October 01, 2016, 02:33:38 AM »
This is really fantastic - thank you for posting it!

One thing I'm having trouble with however:  I can add an FSM template to my player and then have that FSM destroy itself when the "buff" is finished.  But after that buff has expired, I can't get that same template to be applied again to the player. 

I want it so that it only adds the script again IF the buff has expired.  I don't want to use "exists" to add another template, rather I want the buff to be applied, run its full course, and then be able to be applied again.

Any ideas?  Am I missing something?

Here's my setup in case it's something in my setup.

« Last Edit: October 01, 2016, 12:29:12 PM by MarkD »

ransomink

  • Playmaker Newbie
  • *
  • Posts: 44
Re: Add FSM Template at Runtime?
« Reply #20 on: October 08, 2016, 05:28:38 AM »
Last but not least you can optionally send variables to the newly added template. This will initially disable the FSM, add the template, change the variables, then enable the FSM so it can properly start with the variables added.

Code: [Select]
using System.Collections.Generic;
using UnityEngine;

namespace HutongGames.PlayMaker.Actions {
[ActionCategory(ActionCategory.StateMachine)]
public class AddFsmTemplate : FsmStateAction {
[RequiredField]
public FsmOwnerDefault gameObject;
[RequiredField]
public FsmTemplate template;
public FsmString name;
public FsmBool active;
public FsmBool unique;
[CompoundArray("Variables", "Name", "Variable")]
[RequiredField]
public FsmString[] variableNames;
[RequiredField]
public FsmVar[] variables;

private GameObject previousGo;
private List<PlayMakerFSM> fsms;

public override void Reset() {
gameObject = null;
template = null;
name = new FsmString { UseVariable = true };
active = new FsmBool { Value = true };
unique = new FsmBool { Value = false };
variableNames = new FsmString[0];
variables = new FsmVar[0];
}

public override void OnEnter() {
var go = Fsm.GetOwnerDefaultTarget( gameObject );

if ( go == null ) {
return;
}

bool exists = false;

if ( ! unique.Value ) {
if ( go != previousGo ) {
fsms = new List<PlayMakerFSM>();

fsms.AddRange( go.GetComponents<PlayMakerFSM>() );

previousGo = go;
}

if ( fsms.Count > 0 ) foreach ( PlayMakerFSM fsm in fsms ) {
if ( ( ( name.Value != "" ) && ( fsm.FsmName == name.Value ) ) || ( ( fsm.FsmTemplate != null ) && ( fsm.FsmTemplate.name == template.name ) ) ) {
exists = true;
}
}
}

if ( ! exists ) {
PlayMakerFSM newFsm = go.AddComponent<PlayMakerFSM>();

if ( name.Value != "" ) {
newFsm.FsmName = name.Value;
}

if ( ( ! active.Value ) || ( variableNames.Length > 0 ) ) {
newFsm.enabled = false;
}

newFsm.SetFsmTemplate( template );

if ( variableNames.Length > 0 ) {
if ( variableNames.Length > 0 ) for ( int i = 0; i < variableNames.Length; i++ ) {
if ( ! variableNames[i].IsNone ) {
NamedVariable target = newFsm.Fsm.Variables.GetVariable( variableNames[i].Value );

if ( target != null ) {
variables[i].ApplyValueTo( target );
}
}
}

if ( active.Value && ( ! newFsm.enabled ) ) {
newFsm.enabled = true;
}
}

if ( ! unique.Value ) {
fsms.Add( newFsm );
}
}

Finish();
}
}
}

Now all that's left is a "DestroyFSM" action to completely get rid of the FSM component. I have yet to figure this one out yet, but once I do this can be used very effectively for spell debuffs, temporary affects like drag on a rigidbody to simulate "slowing", and much much more.

Regarding performance using the above I saw very little loss, if any at all, so it seams perfectly safe to use as needed.

Is there anyone else having trouble getting the variables to update? Adding the template works great, but when I choose variables to use for the template, it does not operate correctly...

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #21 on: October 20, 2016, 02:24:33 AM »
Hi,

 the variable is only used on Enter, it doesn't udpdate everyframe.

 For this you'll need to use GetFsmXXX and GetHost or GetHostInfo from the Ecosystem, and then your template can get fsm variables this way ( AND set variables too). If you want to decouple the variable name, have as public variable the names of the variables, and then within your template you use them names to access variables, then your template doesn't have to know the variables names of the host.


Bye,

 Jean

abend

  • Playmaker Newbie
  • *
  • Posts: 36
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #22 on: October 02, 2019, 04:09:50 PM »
I am using this script to add a template at runtime from a loop to 4 or 5 game objects. I have 1 variable on my template called Target which is a GameObject. When running, sometimes the variable is populated with what I expect, other times all variables are None, and sometimes the middle of the loop objects are populated, bnut the first and last object are none. I see that there are paired variable arrays here. When I step through, it always seems to populate correctly, but when I run it freely, it's hit or miss. Is there another aspect to this that I am not understanding?

UPDATE: I have skipped the variables section in this script and run set fsm variable right after this. It seems to be working now.

Thanks!
« Last Edit: October 02, 2019, 05:05:37 PM by abend »

600

  • Beta Group
  • Hero Member
  • *
  • Posts: 713
    • Flashing Lights
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #23 on: March 11, 2020, 08:41:26 AM »
Thank you for the action!

Would be convenient to have it on Ecosystem ;D

RDroste

  • Playmaker Newbie
  • *
  • Posts: 2
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #24 on: May 21, 2020, 08:42:56 AM »
Yes can we get this on the ecosystem please

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #25 on: May 26, 2020, 03:04:26 AM »
Hi,

 done, it's on the Ecosystem :)

Bye,

 Jean

Jeremy

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #26 on: July 24, 2020, 11:03:35 PM »
I made an Action on my side before seen yours.
My needs wasn't the same and I saw yours after, so I integrated your code to mine because it could be usefull.
The purpose of my Action was to add FSM template at runtime with system events activated. because with a simple Action to add the FSM Template, it works in Editor Playmode but not in Build. With my Action it works fine.
I have just tested MouseEvents because it's what I needed for my project for the moment but I have implemented the other Events for later needs, I think the problem will be like for the MouseEvent and the solution too.

I share it, if you want to take a look and update your Action.

Custom Action Editor: Editor/AddFSMTemplateToGameObjectEditor.cs
Code: [Select]
// action created by Jeremy Sebagh - 20200720
// modified by Jeremy Sebagh - 20200725

using UnityEngine;
using UnityEditor;
using HutongGames.PlayMaker.Actions;
using HutongGames.PlayMakerEditor;

[CustomActionEditor(typeof(AddFSMTemplateToGameObject))]
public class AddFSMTemplateToGameObjectEditor : CustomActionEditor
{
public override void OnEnable()
{
}

public override bool OnGUI()
{
var isDirty = false;
var action = target as AddFSMTemplateToGameObject;

GUILayout.Label("Select the GameObject and the FSM Template to Add:", EditorStyles.boldLabel);

EditField("_GameObject");
EditField("_FSMTemplate");
GUILayout.Space(3);
EditField("_FSMName");
GUILayout.Space(3);
EditField("_StoreComponent");

GUILayout.Space(5);
GUILayout.Label("Access to Optionals setup", EditorStyles.boldLabel);
EditField("_EnableOptions");
if (action._EnableOptions.Value)
{
GUILayout.Space(3);
EditField("active");
EditField("unique");
EditField("replace");
EditField("variableNames");
EditField("variables");
EditField("existsEventTarget");
EditField("existsSendEvent");
EditField("doNotDestroy");
EditField("replaceEventTarget");
EditField("replaceSendEvent");
}

GUILayout.Space(5);
GUILayout.Label("Access to Events Overrides to allow system events during build runtime", EditorStyles.boldLabel);
EditField("_EnableRuntimeEventsOverride");
if (action._EnableRuntimeEventsOverride.Value)
{
GUILayout.Space(3);
EditField("_MouseEvents");
EditField("_ApplicationEvents");
EditField("_TriggerEnter");
EditField("_TriggerExit");
EditField("_TriggerStay");
EditField("_CollisionEnter");
EditField("_CollisionExit");
EditField("_CollisionStay");
EditField("_LateUpdate");
EditField("_FixedUpdate");
}
return isDirty || GUI.changed;
}
}

Action: AddFSMTemplateToGameObject.cs
Code: [Select]
// action created by Jeremy Sebagh - 20200720
// modified by Jeremy Sebagh - 20200725
// added stuff from action by Krileon: https://hutonggames.com/playmakerforum/index.php?topic=5819.msg28493#msg28493


using UnityEngine;
using System.Collections.Generic;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("0MyCustomActions")]
[Tooltip("Add a FSM Template to a GameObject")]
public class AddFSMTemplateToGameObject : FsmStateAction
{
[RequiredField]
[CheckForComponent(typeof(GameObject))]
[Tooltip("GameObject")]
public FsmOwnerDefault _GameObject;

[RequiredField]
[CheckForComponent(typeof(FsmTemplate))]
[Tooltip("FSM Template")]
public FsmTemplate _FSMTemplate;

[Tooltip("Enable Options")]
public FsmBool _EnableOptions;

[Tooltip("FSM Name")]
public FsmString _FSMName;

[Tooltip("Store Component")]
[ObjectType(typeof(PlayMakerFSM)), UIHint(UIHint.Variable)]
public FsmObject _StoreComponent;
// =====================================
// Code from Krileon Action
// =====================================
public FsmBool active;
public FsmBool unique;
public FsmBool replace;
[CompoundArray("Variables", "Name", "Variable")]
[RequiredField]
public FsmString[] variableNames;
[RequiredField]
public FsmVar[] variables;
// [ObjectType(typeof(PlayMakerFSM)), UIHint(UIHint.Variable)]
// public FsmObject storeComponent;
[ActionSection("Exists Event")]
public FsmEventTarget existsEventTarget;
public FsmString existsSendEvent;
[ActionSection("Replace Event")]
public FsmBool doNotDestroy;
public FsmEventTarget replaceEventTarget;
public FsmString replaceSendEvent;
// =====================================
// End Code from Krileon Action
// =====================================

private GameObject previousGo;
private List<PlayMakerFSM> fsms;


[Tooltip("Enable Events Override")]
public FsmBool _EnableRuntimeEventsOverride;

[Tooltip("Mouse Events")]
public FsmBool _MouseEvents;

[Tooltip("Application Events")]
public FsmBool _ApplicationEvents;

[Tooltip("Trigger Enter")]
public FsmBool _TriggerEnter;

[Tooltip("Trigger Exit")]
public FsmBool _TriggerExit;

[Tooltip("Trigger Stay")]
public FsmBool _TriggerStay;

[Tooltip("Collision Enter")]
public FsmBool _CollisionEnter;

[Tooltip("Collision Exit")]
public FsmBool _CollisionExit;

[Tooltip("Collision Stay")]
public FsmBool _CollisionStay;

[Tooltip("Collision Stay")]
public FsmBool _LateUpdate;

[Tooltip("Collision Stay")]
public FsmBool _FixedUpdate;

public override void Reset()
{
_GameObject = null;
_FSMTemplate = null;
_EnableOptions = null;
_FSMName = null;
_StoreComponent = null;
_EnableRuntimeEventsOverride = null;
_MouseEvents = false;
_ApplicationEvents = false;
_TriggerEnter = false;
_TriggerExit = false;
_TriggerStay = false;
_CollisionEnter = false;
_CollisionExit = false;
_CollisionStay = false;
_LateUpdate = false;
_FixedUpdate = false;

// =====================================
// Code from Krileon Action
// =====================================
active = new FsmBool { Value = true };
unique = new FsmBool { Value = false };
replace = new FsmBool { Value = false };
variableNames = new FsmString[0];
variables = new FsmVar[0];
existsEventTarget = null;
existsSendEvent = null;
doNotDestroy = new FsmBool { Value = false };
replaceEventTarget = null;
replaceSendEvent = null;
// =====================================
// End Code from Krileon Action
// =====================================
}
public override void OnEnter()
{
GameObject gameObject = Fsm.GetOwnerDefaultTarget(_GameObject);
if (gameObject == null)
{
Finish();
return;
}

PlayMakerFSM FSMTemplate = null;

if (_EnableOptions.Value)
{
// ==============================================================
// Modified Code from Krileon Action (only some variable names)
// ==============================================================

bool exists = false;

if ((!unique.Value) || replace.Value)
{
if (gameObject != previousGo)
{
fsms = new List<PlayMakerFSM>();

fsms.AddRange(gameObject.GetComponents<PlayMakerFSM>());

previousGo = gameObject;
}

if (fsms.Count > 0) foreach (PlayMakerFSM fsm in fsms)
{
if (((_FSMName.Value != "") && (fsm.FsmName == _FSMName.Value)) || ((fsm.FsmTemplate != null) && (fsm.FsmTemplate.name == _FSMTemplate.name)))
{
if (replace.Value)
{
if (replaceSendEvent.Value != "")
{
Fsm.Event(replaceEventTarget, replaceSendEvent.Value);
}

if (!doNotDestroy.Value)
{
Object.Destroy(fsm);
}
}
else
{
_StoreComponent.Value = fsm;
exists = true;
}
}
}
}

if (!exists)
{
FSMTemplate = gameObject.AddComponent<PlayMakerFSM>();

if (_FSMName.Value != "")
{
FSMTemplate.FsmName = _FSMName.Value;
}

if ((!active.Value) || (variableNames.Length > 0))
{
FSMTemplate.enabled = false;
}

FSMTemplate.SetFsmTemplate(_FSMTemplate);

if (variableNames.Length > 0)
{
if (variableNames.Length > 0) for (int i = 0; i < variableNames.Length; i++)
{
if (!variableNames[i].IsNone)
{
NamedVariable target = FSMTemplate.Fsm.Variables.GetVariable(variableNames[i].Value);

if (target != null)
{
variables[i].ApplyValueTo(target);
}
}
}

if (active.Value && (!FSMTemplate.enabled))
{
FSMTemplate.enabled = true;
}
}

if ((!unique.Value) || replace.Value)
{
fsms.Add(FSMTemplate);
}

_StoreComponent.Value = FSMTemplate;
}
else
{
if (existsSendEvent.Value != "")
{
Fsm.Event(existsEventTarget, existsSendEvent.Value);
}
}
// =====================================
// End Modified Code from Krileon Action
// =====================================
}
else
{
FSMTemplate = gameObject.AddComponent<PlayMakerFSM>();
FSMTemplate.SetFsmTemplate(_FSMTemplate);
FSMTemplate.FsmName = _FSMName.Value;
_StoreComponent.Value = FSMTemplate;
}

if (_EnableRuntimeEventsOverride.Value)
{
bool exists = false;

if (_MouseEvents.Value)
{
exists = false;
FSMTemplate.Fsm.MouseEvents = _MouseEvents.Value;
PlayMakerMouseEvents thisMouseEvent;
if (gameObject.GetComponent<PlayMakerMouseEvents>() != null)
{
List<PlayMakerMouseEvents> CheckMouseEvents = new List<PlayMakerMouseEvents>();
CheckMouseEvents.AddRange(gameObject.GetComponents<PlayMakerMouseEvents>());
foreach (PlayMakerMouseEvents MouseEvent in CheckMouseEvents)
{
if (MouseEvent.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisMouseEvent = gameObject.GetComponent<PlayMakerMouseEvents>(); } else { thisMouseEvent = null; }
}
else
{
thisMouseEvent = gameObject.AddComponent<PlayMakerMouseEvents>();
}
if (thisMouseEvent) thisMouseEvent.TargetFSMs.Add(FSMTemplate);
}
if (_ApplicationEvents.Value)
{
exists = false;
FSMTemplate.Fsm.HandleApplicationEvents = _ApplicationEvents.Value;
PlayMakerApplicationEvents thisApplicationEvent;
if (gameObject.GetComponent<PlayMakerApplicationEvents>() != null)
{
List<PlayMakerApplicationEvents> CheckApplicationEvents = new List<PlayMakerApplicationEvents>();
CheckApplicationEvents.AddRange(gameObject.GetComponents<PlayMakerApplicationEvents>());
foreach (PlayMakerApplicationEvents ApplicationEvent in CheckApplicationEvents)
{
if (ApplicationEvent.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisApplicationEvent = gameObject.GetComponent<PlayMakerApplicationEvents>(); } else { thisApplicationEvent = null; }
}
else
{
thisApplicationEvent = gameObject.AddComponent<PlayMakerApplicationEvents>();
}
if (thisApplicationEvent) thisApplicationEvent.TargetFSMs.Add(FSMTemplate);
}
if (_TriggerEnter.Value)
{
exists = false;
FSMTemplate.Fsm.HandleTriggerEnter = _TriggerEnter.Value;
PlayMakerTriggerEnter thisTriggerEnter;
if (gameObject.GetComponent<PlayMakerTriggerEnter>() != null)
{
List<PlayMakerTriggerEnter> CheckTriggerEnter = new List<PlayMakerTriggerEnter>();
CheckTriggerEnter.AddRange(gameObject.GetComponents<PlayMakerTriggerEnter>());
foreach (PlayMakerTriggerEnter TriggerEnter in CheckTriggerEnter)
{
if (TriggerEnter.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisTriggerEnter = gameObject.GetComponent<PlayMakerTriggerEnter>(); } else { thisTriggerEnter = null; }
}
else
{
thisTriggerEnter = gameObject.AddComponent<PlayMakerTriggerEnter>();
}
if (thisTriggerEnter) thisTriggerEnter.TargetFSMs.Add(FSMTemplate);
}
if (_TriggerExit.Value)
{
exists = false;
FSMTemplate.Fsm.HandleTriggerExit = _TriggerExit.Value;
PlayMakerTriggerExit thisTriggerExit;
if (gameObject.GetComponent<PlayMakerTriggerExit>() != null)
{
List<PlayMakerTriggerExit> CheckTriggerExit = new List<PlayMakerTriggerExit>();
CheckTriggerExit.AddRange(gameObject.GetComponents<PlayMakerTriggerExit>());
foreach (PlayMakerTriggerExit TriggerExit in CheckTriggerExit)
{
if (TriggerExit.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisTriggerExit = gameObject.GetComponent<PlayMakerTriggerExit>(); } else { thisTriggerExit = null; }
}
else
{
thisTriggerExit = gameObject.AddComponent<PlayMakerTriggerExit>();
}
if (thisTriggerExit) thisTriggerExit.TargetFSMs.Add(FSMTemplate);
}
if (_TriggerStay.Value)
{
exists = false;
FSMTemplate.Fsm.HandleTriggerStay = _TriggerStay.Value;
PlayMakerTriggerStay thisTriggerStay;
if (gameObject.GetComponent<PlayMakerTriggerStay>() != null)
{
List<PlayMakerTriggerStay> CheckTriggerStay = new List<PlayMakerTriggerStay>();
CheckTriggerStay.AddRange(gameObject.GetComponents<PlayMakerTriggerStay>());
foreach (PlayMakerTriggerStay TriggerStay in CheckTriggerStay)
{
if (TriggerStay.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisTriggerStay = gameObject.GetComponent<PlayMakerTriggerStay>(); } else { thisTriggerStay = null; }
}
else
{
thisTriggerStay = gameObject.AddComponent<PlayMakerTriggerStay>();
}
if (thisTriggerStay) thisTriggerStay.TargetFSMs.Add(FSMTemplate);
}
if (_CollisionEnter.Value)
{
exists = false;
FSMTemplate.Fsm.HandleCollisionEnter = _CollisionEnter.Value;
PlayMakerCollisionEnter thisCollisionEnter;
if (gameObject.GetComponent<PlayMakerCollisionEnter>() != null)
{
List<PlayMakerCollisionEnter> CheckCollisionEnter = new List<PlayMakerCollisionEnter>();
CheckCollisionEnter.AddRange(gameObject.GetComponents<PlayMakerCollisionEnter>());
foreach (PlayMakerCollisionEnter CollisionEnter in CheckCollisionEnter)
{
if (CollisionEnter.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisCollisionEnter = gameObject.GetComponent<PlayMakerCollisionEnter>(); } else { thisCollisionEnter = null; }
}
else
{
thisCollisionEnter = gameObject.AddComponent<PlayMakerCollisionEnter>();
}
if (thisCollisionEnter) thisCollisionEnter.TargetFSMs.Add(FSMTemplate);
}
if (_CollisionExit.Value)
{
exists = false;
FSMTemplate.Fsm.HandleCollisionExit = _CollisionExit.Value;
PlayMakerCollisionExit thisCollisionExit;
if (gameObject.GetComponent<PlayMakerCollisionExit>() != null)
{
List<PlayMakerCollisionExit> CheckCollisionExit = new List<PlayMakerCollisionExit>();
CheckCollisionExit.AddRange(gameObject.GetComponents<PlayMakerCollisionExit>());
foreach (PlayMakerCollisionExit CollisionExit in CheckCollisionExit)
{
if (CollisionExit.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisCollisionExit = gameObject.GetComponent<PlayMakerCollisionExit>(); } else { thisCollisionExit = null; }
}
else
{
thisCollisionExit = gameObject.AddComponent<PlayMakerCollisionExit>();
}
if (thisCollisionExit) thisCollisionExit.TargetFSMs.Add(FSMTemplate);
}
if (_CollisionStay.Value)
{
exists = false;
FSMTemplate.Fsm.HandleCollisionStay = _CollisionStay.Value;
PlayMakerCollisionStay thisCollisionStay;
if (gameObject.GetComponent<PlayMakerCollisionStay>() != null)
{
List<PlayMakerCollisionStay> CheckCollisionStay = new List<PlayMakerCollisionStay>();
CheckCollisionStay.AddRange(gameObject.GetComponents<PlayMakerCollisionStay>());
foreach (PlayMakerCollisionStay CollisionStay in CheckCollisionStay)
{
if (CollisionStay.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisCollisionStay = gameObject.GetComponent<PlayMakerCollisionStay>(); } else { thisCollisionStay = null; }
}
else
{
thisCollisionStay = gameObject.AddComponent<PlayMakerCollisionStay>();
}
if (thisCollisionStay) thisCollisionStay.TargetFSMs.Add(FSMTemplate);
}
if (_LateUpdate.Value)
{
exists = false;
FSMTemplate.Fsm.HandleLateUpdate = _LateUpdate.Value;
PlayMakerLateUpdate thisLateUpdate;
if (gameObject.GetComponent<PlayMakerLateUpdate>() != null)
{
List<PlayMakerLateUpdate> CheckLateUpdate = new List<PlayMakerLateUpdate>();
CheckLateUpdate.AddRange(gameObject.GetComponents<PlayMakerLateUpdate>());
foreach (PlayMakerLateUpdate LateUpdate in CheckLateUpdate)
{
if (LateUpdate.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisLateUpdate = gameObject.GetComponent<PlayMakerLateUpdate>(); } else { thisLateUpdate = null; }
}
else
{
thisLateUpdate = gameObject.AddComponent<PlayMakerLateUpdate>();
}
if (thisLateUpdate) thisLateUpdate.TargetFSMs.Add(FSMTemplate);
}
if (_FixedUpdate.Value)
{
exists = false;
FSMTemplate.Fsm.HandleFixedUpdate = _FixedUpdate.Value;
PlayMakerFixedUpdate thisFixedUpdate;
if (gameObject.GetComponent<PlayMakerFixedUpdate>() != null)
{
List<PlayMakerFixedUpdate> CheckFixedUpdate = new List<PlayMakerFixedUpdate>();
CheckFixedUpdate.AddRange(gameObject.GetComponents<PlayMakerFixedUpdate>());
foreach (PlayMakerFixedUpdate FixedUpdate in CheckFixedUpdate)
{
if (FixedUpdate.TargetFSMs.Contains(FSMTemplate))
{
exists = true;
break;
}
}

if (!exists) { thisFixedUpdate = gameObject.GetComponent<PlayMakerFixedUpdate>(); } else { thisFixedUpdate = null; }
}
else
{
thisFixedUpdate = gameObject.AddComponent<PlayMakerFixedUpdate>();
}
if (thisFixedUpdate) thisFixedUpdate.TargetFSMs.Add(FSMTemplate);
}
}
Finish();
}
}
}

edit: I posted it on the bad topic , it's corrected ;D
edit2: Warning Correction
edit3: New Correction
« Last Edit: July 26, 2020, 03:37:00 AM by Jeremy »

manuelprcarvalho

  • Playmaker Newbie
  • *
  • Posts: 1
Re: Add FSM Template at Runtime? [SOLVED]
« Reply #27 on: December 07, 2023, 08:18:40 AM »
Hi all,
I know this is a super old topic, but I'm having trouble with this action in Unity 2022, it works flawlessly on Unity 2021 but throws a few null reference errors whenever I try to run it in 2022:

 
Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
PlayMakerFSM.Awake () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:231)
UnityEngine.GameObject:AddComponent()
HutongGames.PlayMaker.Actions.AddFsmTemplate:OnEnter() (at Assets/PlayMaker Custom Actions/StateMachine/AddFsmTemplate.cs:91)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:DoTransition(FsmTransition, Boolean) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3005)
HutongGames.PlayMaker.Fsm:ProcessEvent(FsmEvent, FsmEventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2531)
HutongGames.PlayMaker.Fsm:SendEventToFsmOnGameObject(GameObject, String, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2936)
HutongGames.PlayMaker.Fsm:Event(FsmEventTarget, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2672)
HutongGames.PlayMaker.Actions.SendEvent:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:Start() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2192)
HutongGames.PlayMaker.Actions.RunFSM:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/RunFSM.cs:75)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:Start() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2192)
HutongGames.PlayMaker.Actions.RunFSM:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/RunFSM.cs:75)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:DoTransition(FsmTransition, Boolean) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3005)
HutongGames.PlayMaker.Fsm:ProcessEvent(FsmEvent, FsmEventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2531)
HutongGames.PlayMaker.Fsm:Event(FsmEventTarget, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2659)
HutongGames.PlayMaker.Fsm:Event(FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2739)
HutongGames.PlayMaker.Fsm:Event(GameObject, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2627)
HutongGames.PlayMaker.PlayMakerUiEventBase:SendEvent(FsmEvent) (at Assets/PlayMaker/Actions/UI/Components/PlayMakerUiEventBase.cs:58)
HutongGames.PlayMaker.PlayMakerUiClickEvent:DoOnClick() (at Assets/PlayMaker/Actions/UI/Components/PlayMakerUiClickEvent.cs:41)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)

Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
PlayMakerFSM.OnDisable () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:691)
UnityEngine.GameObject:AddComponent()
HutongGames.PlayMaker.Actions.AddFsmTemplate:OnEnter() (at Assets/PlayMaker Custom Actions/StateMachine/AddFsmTemplate.cs:91)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:DoTransition(FsmTransition, Boolean) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3005)
HutongGames.PlayMaker.Fsm:ProcessEvent(FsmEvent, FsmEventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2531)
HutongGames.PlayMaker.Fsm:SendEventToFsmOnGameObject(GameObject, String, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2936)
HutongGames.PlayMaker.Fsm:Event(FsmEventTarget, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2672)
HutongGames.PlayMaker.Actions.SendEvent:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:Start() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2192)
HutongGames.PlayMaker.Actions.RunFSM:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/RunFSM.cs:75)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:Start() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2192)
HutongGames.PlayMaker.Actions.RunFSM:OnEnter() (at Assets/PlayMaker/Actions/StateMachine/RunFSM.cs:75)
HutongGames.PlayMaker.FsmState:ActivateActions(Int32) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState:OnEnter() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm:EnterState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm:SwitchState(FsmState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm:UpdateStateChanges() (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm:DoTransition(FsmTransition, Boolean) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3005)
HutongGames.PlayMaker.Fsm:ProcessEvent(FsmEvent, FsmEventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2531)
HutongGames.PlayMaker.Fsm:Event(FsmEventTarget, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2659)
HutongGames.PlayMaker.Fsm:Event(FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2739)
HutongGames.PlayMaker.Fsm:Event(GameObject, FsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2627)
HutongGames.PlayMaker.PlayMakerUiEventBase:SendEvent(FsmEvent) (at Assets/PlayMaker/Actions/UI/Components/PlayMakerUiEventBase.cs:58)
HutongGames.PlayMaker.PlayMakerUiClickEvent:DoOnClick() (at Assets/PlayMaker/Actions/UI/Components/PlayMakerUiClickEvent.cs:41)
UnityEngine.EventSystems.EventSystem:Update() (at ./Library/PackageCache/com.unity.ugui@1.0.0/Runtime/EventSystem/EventSystem.cs:530)

Code: [Select]
NullReferenceException: Object reference not set to an instance of an object
HutongGames.PlayMaker.Actions.AddFsmTemplate.OnEnter () (at Assets/PlayMaker Custom Actions/StateMachine/AddFsmTemplate.cs:94)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm.DoTransition (HutongGames.PlayMaker.FsmTransition transition, System.Boolean isGlobal) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3005)
HutongGames.PlayMaker.Fsm.ProcessEvent (HutongGames.PlayMaker.FsmEvent fsmEvent, HutongGames.PlayMaker.FsmEventData eventData) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2531)
HutongGames.PlayMaker.Fsm.SendEventToFsmOnGameObject (UnityEngine.GameObject gameObject, System.String fsmName, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2936)
HutongGames.PlayMaker.Fsm.Event (HutongGames.PlayMaker.FsmEventTarget eventTarget, HutongGames.PlayMaker.FsmEvent fsmEvent) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2672)
HutongGames.PlayMaker.Actions.SendEvent.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/SendEvent.cs:42)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3096)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:3035)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2968)
HutongGames.PlayMaker.Fsm.Start () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2192)
HutongGames.PlayMaker.Actions.RunFSM.OnEnter () (at Assets/PlayMaker/Actions/StateMachine/RunFSM.cs:75)
HutongGames.PlayMaker.FsmState.ActivateActions (System.Int32 startIndex) (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:229)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.1/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:199)

this action is great but unfortunately I have to use Unity 2022 for my current project, any help would be super appreciated.