playMaker

Author Topic: AddToFsmInt.cs  (Read 5915 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
AddToFsmInt.cs
« on: June 17, 2011, 06:19:46 AM »
Hi,

 Following a post ( http://hutonggames.com/playmakerforum/index.php?topic=351.0 ),

please find a custom action ( really just a mix of the existing ones) that allow you to add to an int from another fsm without too much trouble creating events, states, etc etc. You can also store the result locally if you need it.

Code: [Select]
// (c) Copyright HutongGames, LLC 2010-2011. All rights reserved.

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.Math)]
[Tooltip("Adds a value to an fsm Integer Variable.")]
public class AddToFsmInt : FsmStateAction
{

[RequiredField]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.FsmName)]
[Tooltip("Optional name of FSM on Game Object")]
public FsmString fsmName;
[RequiredField]
[UIHint(UIHint.FsmInt)]
public FsmString variableName;

[RequiredField]
public FsmInt add;

[Tooltip("Optional storage of the result")]
[UIHint(UIHint.Variable)]
public FsmInt storeResult;


public bool everyFrame;



GameObject goLastFrame;
PlayMakerFSM fsm;

public override void Reset()
{
gameObject = null;
add = null;
fsmName = "";
storeResult = null;
}

public override void OnEnter()
{
DoAddToFsmInt();

if (!everyFrame)
Finish();
}

public override void OnUpdate()
{
DoAddToFsmInt();
}

void DoAddToFsmInt()
{

GameObject go = Fsm.GetOwnerDefaultTarget(gameObject);
if (go == null) return;

// only get the fsm component if go has changed

if (go != goLastFrame)
{
goLastFrame = go;
fsm = ActionHelpers.GetGameObjectFsm(go, fsmName.Value);
}

if (fsm == null) return;

FsmInt fsmInt = fsm.FsmVariables.GetFsmInt(variableName.Value);

if (fsmInt == null) return;

fsmInt.Value += add.Value;

if (storeResult != null){
storeResult.Value = fsmInt.Value;
}

}// DoAddToFsmInt
}
}

Bye,

 Jean

LogLady

  • Full Member
  • ***
  • Posts: 150
Re: AddToFsmInt.cs
« Reply #1 on: November 20, 2015, 08:17:09 AM »
It is very useful and should be on the Ecosystem.

Thanks, Jean!

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: AddToFsmInt.cs
« Reply #2 on: November 23, 2015, 08:16:26 AM »
Done :)

 Bye,

 Jean

LogLady

  • Full Member
  • ***
  • Posts: 150
Re: AddToFsmInt.cs
« Reply #3 on: November 23, 2015, 02:47:27 PM »
\o/

Thanks!!!

LuminRabbit

  • Full Member
  • ***
  • Posts: 161
  • Lumin Rabbit
Re: AddToFsmInt.cs
« Reply #4 on: October 05, 2020, 04:33:53 PM »
Ok this is way Cooler !!! and sure makes life easy :D
Thank you Jean!
Have I said how much I love playmaker!!! This is one amazing tool