Hi,
This generic iteration action will be very useful. It simply iterate between a start and end integer, you can then do whatever you like during the loop and act based on the current index.
[EDIT]
It can count up and down ( if end value is less or more than start value), and you can define the increment step, for example you can increment every 2, instead of every 1
Also found a small issue where the action would not go trough the first value, this is now corrected and will properly start at the start value, instead of start value + increment.
// (c) Copyright HutongGames, LLC 2010-2012. All rights reserved.
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.StateMachine)]
[Tooltip("Each time this action is called it iterate to the next value from Start to End. This lets you safely loop and process anything on each iteratation.")]
public class Iterate : FsmStateAction
{
[RequiredField]
[Tooltip("Start value")]
public FsmInt startIndex;
[Tooltip("End value")]
public FsmInt endIndex;
[Tooltip("increment value at each iteration, absolute value only, it will itself find if it needs to substract or add")]
public FsmInt increment;
[Tooltip("Event to send to get the next child.")]
public FsmEvent loopEvent;
[Tooltip("Event to send when we reached the end.")]
public FsmEvent finishedEvent;
[ActionSection("Result")]
[Tooltip("The current value of the iteration process")]
[UIHint(UIHint.Variable)]
public FsmInt currentIndex;
private bool started = false;
private bool _up = true;
public override void Reset()
{
startIndex = 0;
endIndex = 10;
currentIndex = null;
loopEvent = null;
finishedEvent = null;
increment = 1;
}
public override void OnEnter()
{
DoGetNext();
Finish();
}
void DoGetNext()
{
// reset?
if (!started)
{
_up = startIndex.Value<endIndex.Value;
currentIndex.Value = startIndex.Value;
started = true;
if (loopEvent != null)
{
Fsm.Event(loopEvent);
}
return;
}
if (_up)
{
if (currentIndex.Value >= endIndex.Value)
{
started = false;
Fsm.Event(finishedEvent);
return;
}
// iterate
currentIndex.Value = Mathf.Max(startIndex.Value,currentIndex.Value+Mathf.Abs(increment.Value));
}else{
if (currentIndex.Value <= endIndex.Value)
{
started = false;
Fsm.Event(finishedEvent);
return;
}
// iterate
currentIndex.Value = Mathf.Max(endIndex.Value,currentIndex.Value -Mathf.Abs(increment.Value));
}
if (loopEvent != null)
{
Fsm.Event(loopEvent);
}
}
}
}
Bye,
Jean