playMaker

Author Topic: ArrayMaker find closest gameObject  (Read 4131 times)

kiriri

  • Hero Member
  • *****
  • Posts: 506
ArrayMaker find closest gameObject
« on: December 26, 2012, 08:56:39 AM »
Peace!
sure this is easily possible with just the actions we have already, but it's also something one does quite a lot so I thought a dedicated action may speed things up, at least it does so for me. Additionally, it's always faster to use scripts, though in this case I made some additional optimizations (y'know, hello world style optimisations :D Like it uses sqrMagnitude instead of Magnitude/Distance etc).
while I'm at this, kudos to jean for making his plugin available for free and for everyone :)
« Last Edit: December 26, 2012, 10:05:32 AM by kiriri »
Best,
Sven

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: ArrayMaker find closest gameObject
« Reply #1 on: January 02, 2013, 05:45:02 AM »
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.

Code: [Select]
// (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

kiriri

  • Hero Member
  • *****
  • Posts: 506
Re: ArrayMaker find closest gameObject
« Reply #2 on: January 02, 2013, 06:07:11 AM »
Thanks Jean, that looks very tidysome! :D
As you have probably noticed, I pretty much just copied the one function from your getNext action, and I was so happy to get it working on the first try that I did not really care about order xD . But it did work without intermediate transitions. My effort demands that I note that :P
Best,
Sven

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: ArrayMaker find closest gameObject
« Reply #3 on: January 02, 2013, 06:13:38 AM »
Hi Sven,

 Yes, such initiative is real cool of you indeed! We all know how involving and time consuming development and programming is... for sure!

bye,

 Jean