Hi Kiriri,
Cool! and thanks for the credits, much appreciated!
Regards your action, I would actually simply make a routine within one function. The concept of iterating through a list of items using PlayMaker events system will work fine, but since you wrote an action for that, you could save the user having to create the looping event and state, as it would not have any meaning then. so the action below will do the same but will work as is without requiring any transition wiring or intermediate states.
// (c) Jean Fabre, 2012 All rights reserved.
// http://www.fabrejean.net
// contact: http://www.fabrejean.net/contact.htm
//
// Version Alpha 0.9
// INSTRUCTIONS
// Drop a PlayMakerArrayList script onto a GameObject, and define a unique name for reference if several PlayMakerArrayList coexists on that GameObject.
// In this Action interface, link that GameObject in "arrayListObject" and input the reference name if defined.
// Note: You can directly reference that GameObject or store it in an Fsm variable or global Fsm variable
using UnityEngine;
namespace HutongGames.PlayMaker.Actions
{
[ActionCategory("ArrayMaker/ArrayList")]
[Tooltip("Return the closest GameObject within an arrayList from a transform or position.")]
public class ArrayListGetClosestGameObject : ArrayListActions
{
[ActionSection("Set up")]
[RequiredField]
[Tooltip("The gameObject with the PlayMaker ArrayList Proxy component")]
[CheckForComponent(typeof(PlayMakerArrayListProxy))]
public FsmOwnerDefault gameObject;
[Tooltip("Author defined Reference of the PlayMaker ArrayList Proxy component ( necessary if several component coexists on the same GameObject")]
public FsmString reference;
[Tooltip("Compare the distance of the items in the list to the position of this gameObject")]
public FsmGameObject distanceFrom;
[Tooltip("If DistanceFrom declared, use OrDistanceFromVector3 as an offset")]
public FsmVector3 orDistanceFromVector3;
public bool everyframe;
[ActionSection("Result")]
[UIHint(UIHint.Variable)]
public FsmGameObject closestGameObject;
[UIHint(UIHint.Variable)]
public FsmInt closestIndex;
public override void Reset()
{
gameObject = null;
reference = null;
distanceFrom = null;
orDistanceFromVector3 = null;
closestGameObject = null;
closestIndex = null;
everyframe = true;
}
public override void OnEnter()
{
if (! SetUpArrayListProxyPointer(Fsm.GetOwnerDefaultTarget(gameObject),reference.Value) )
{
Finish();
}
DoFindClosestGo();
if (!everyframe)
{
Finish();
}
}
public override void OnUpdate()
{
DoFindClosestGo();
}
void DoFindClosestGo()
{
if (! isProxyValid())
{
return;
}
Vector3 root = orDistanceFromVector3.Value;
GameObject _rootGo = distanceFrom.Value;
if (_rootGo!=null)
{
root += _rootGo.transform.position;
}
float sqrDist = Mathf.Infinity;
int _index = 0;
float sqrDistTest;
foreach(GameObject _go in proxy.arrayList)
{
if (_go!=null)
{
sqrDistTest = (_go.transform.position - root).sqrMagnitude;
if (sqrDistTest<= sqrDist)
{
sqrDist = sqrDistTest;
closestGameObject.Value = _go;
closestIndex.Value = _index;
}
}
_index++;
}
}
}
}
bye,
Jean