playMaker

Author Topic: generic iterate action available  (Read 7822 times)

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
generic iterate action available
« on: August 08, 2012, 08:32:31 AM »
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.

Code: [Select]
// (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
« Last Edit: October 08, 2012, 01:29:13 AM by jeanfabre »

dudejrtm

  • Playmaker Newbie
  • *
  • Posts: 6
Re: generic iterate action available
« Reply #1 on: August 14, 2012, 01:04:04 PM »
 ;) very useful!! thanx!

choi

  • Playmaker Newbie
  • *
  • Posts: 40
Re: generic iterate action available
« Reply #2 on: August 15, 2012, 08:34:35 AM »
Is a useful feature.
Thank you.

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: generic iterate action available
« Reply #3 on: August 31, 2012, 08:17:14 PM »
This solved my infinite loop problem. You are awesome, Jean!

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: generic iterate action available
« Reply #4 on: October 05, 2012, 04:22:24 PM »
Hey Jean, would you consider adding the ability to count up or down? For example:

count down till X = 0
count up till X = 100

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15594
  • Official Playmaker Support
Re: generic iterate action available
« Reply #5 on: October 08, 2012, 01:30:11 AM »
Hi,

 done, see comments on the original post as well, spotted an error when testing where the start value would be omitted.

bye,

 Jean

amaranth

  • Full Member
  • ***
  • Posts: 172
Re: generic iterate action available
« Reply #6 on: October 08, 2012, 03:51:11 PM »
Thank you!  :D