playMaker

Author Topic: [Action] Get Random Child action with Tag  (Read 2260 times)

Sly

  • Full Member
  • ***
  • Posts: 123
[Action] Get Random Child action with Tag
« on: February 07, 2014, 02:53:38 PM »
Hello,

I just want to do a variant of the action Get Random Child, but I want to add a random into it.
How I can do it? I don't find the syntaxe for adding the tag request.

There is my code. And the ??? will show you where I'm blocked.

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Gets a Random Child of a Game Object.")]
public class GetRandomChildWithTag : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[UIHint(UIHint.Tag)]
[Tooltip("Find a GameObject with this tag. If Object Name is specified then both name and Tag must match.")]
public FsmString withTag;
[RequiredField]
[UIHint(UIHint.Variable)]
public FsmGameObject storeResult;

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

public override void OnEnter()
{
DoGetRandomChild();
Finish();
}

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

int childCount = go.transform.childCount;
if (childCount == 0) return;

if (string.IsNullOrEmpty(withTag.Value) || withTag.Value == "Untagged")
{
storeResult.Value = go.transform.GetChild(Random.Range(0, childCount)).gameObject;
}
else
{
//storeResult.Value = go.transform.GetChild(Random.Range(0, childCount)).gameObject;

???

}




}
}
}

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 15500
  • Official Playmaker Support
Re: [Action] Get Random Child action with Tag
« Reply #1 on: February 11, 2014, 06:49:06 AM »
Hi,

 There isn't any api to do this. Unity doesn't provide a method to GetChild() by tags.

So you will have to do it manually like so:

Code: [Select]
for(var child : Transform in transform){
    if(child.gameObject.tag == "Cover Node"){
        gos2.Add(child);
    }
}

to add some randomization, I would simply store all found children matching the tags in a temporary list, and then take a random one form there.



bye,

 Jean