playMaker

Author Topic: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object  (Read 3871 times)

Drakon

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Hiya, I've just updated to Unity 3.5 and playMaker 1.3.1 and have one state that loops back on itself when finished. Then number of parented children keeps changing with time and I'm trying to store the last or one before last child. But the Game Object variable is not being stored. Any suggestions?

Thanks,
Drakon.

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3634
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #1 on: February 18, 2012, 05:55:41 PM »
There was a problem with GetChild and GetChildNum in 1.3.0, but it was fixed in 1.3.1.

I just ran some tests, and the actions you list seem to be working fine in 1.3.1.

Can you double check that you have 1.3.1 installed (Main Menu > Playmaker > About Playmaker...)

Drakon

  • Playmaker Newbie
  • *
  • Posts: 21
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #2 on: February 19, 2012, 05:22:42 AM »
Hi Alex,

I checked the version - it's 1.3.1(41089). I first deleted the Get Child Num action then re-imported the whole of playMaker from the Asset Store (just importing Get Child Num action by itself didn't work).  

Not sure if it's to do the way I'm setting up the fsm - I'm dropping the parent onto the 'Game Object' slot, 'Child Index' is set to an incrementing int variable and 'Store' is set to a test GameObject (tried both local & global).

Still the same problem. And now I'm also getting:

DivideByZeroException: Division by zero
HutongGames.PlayMaker.Actions.GetChildNum.DoGetChildNum (UnityEngine.GameObject go) (at Assets/PlayMaker/Actions/GetChildNum.cs:37)
HutongGames.PlayMaker.Actions.GetChildNum.OnEnter () (at Assets/PlayMaker/Actions/GetChildNum.cs:30)
HutongGames.PlayMaker.FsmState.OnEnter ()
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state)
HutongGames.PlayMaker.Fsm.Update ()


Is it a good idea to back up and delete playMaker folder then import a fresh install?

Thank You,
Drakon
« Last Edit: February 19, 2012, 06:23:33 AM by Drakon »

szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #3 on: September 17, 2014, 10:23:28 PM »
I started getting a similar? bug with: Unity 4.5.4f1 and Playmaker 1.7.7.f6

Code: [Select]
DivideByZeroException: Division by zero
HutongGames.PlayMaker.Actions.GetChildNum.DoGetChildNum (UnityEngine.GameObject go) (at Assets/PlayMaker/Actions/GetChildNum.cs:40)
HutongGames.PlayMaker.Actions.GetChildNum.OnEnter () (at Assets/PlayMaker/Actions/GetChildNum.cs:33)
HutongGames.PlayMaker.FsmState.ActivateActions (Int32 startIndex)
HutongGames.PlayMaker.FsmState.OnEnter ()
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState)
HutongGames.PlayMaker.Fsm.UpdateStateChanges ()
HutongGames.PlayMaker.Fsm.UpdateState (HutongGames.PlayMaker.FsmState state)
HutongGames.PlayMaker.Fsm.Update ()
PlayMakerFSM.Update ()

When this happens, I am destroying the children of an object by getting child num 0 destroying it then checking if there are still children and if it's more then 0 destroying child num 0 again, etc.

Hmmm, maybe they are not correctly destroyed so fast? I'll try adding a last frame action...

Edit: No, delays don't seem to solve this.
It seems to crash when there are no child objects left, still for some reason I am trying to get child num 0.
But that state should not be activated again as I am checking the child count before it and if 0 "going another way".

And anyway this should not crash the code, right?
« Last Edit: September 17, 2014, 10:48:45 PM by szomaza »

jeanfabre

  • Administrator
  • Hero Member
  • *****
  • Posts: 14703
  • Official Playmaker Support
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #4 on: September 18, 2014, 06:52:42 AM »
Hi,

 when doing this, are you counting backward? if you delete from the last child, then your index will be fine. if you count from 0, then all indexes gets confused as child 1 is not child 0...

Bye,

 Jean

szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #5 on: September 18, 2014, 11:02:23 PM »
Hi,

I just managed to figure it out that it was my fault in the first place that this situation (looking for child #0 when there is none) could even occur.

In one state I checked the child count and if more then 0 then in the next state used child number 0 for something.
The problem was that I was also modifying child parenting in another FSM simultaneously and quite frequently in the short time-frame (is that 1 frame?) between the 2 states it removed that last child.

So that's solved.
But still when there are no child objects and you try to get child number it should not cause a crash, right?

Jean, your suggestion (count backwards) makes lots of sense, but is that really necessary?
When you get child #0 and parent it to something else or destroy it, doesn't the previous child #1 become #0, etc?
I have been doing these things, like removing all the children from an object this way* without any problem.

*(check child count, if not 0 then remove child #0 and repeat this until child count=0)

Thanks,
szomaza

szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #6 on: December 23, 2018, 01:05:57 AM »
Apparently it seems like 4 years have passed and I am still here having fun with Playmaker.  :-)
I just came across this same problem I had before and I still can't explain why it is happening, but I have an easy repro which should exactly tell somebody who knows more then me what is going on and how this should be fixed from the code side.

I get the following error message:
Code: [Select]
DivideByZeroException: Division by zero
HutongGames.PlayMaker.Actions.GetChildNum.DoGetChildNum (UnityEngine.GameObject go) (at Assets/PlayMaker/Actions/GameObject/GetChildNum.cs:40)
HutongGames.PlayMaker.Actions.GetChildNum.OnEnter () (at Assets/PlayMaker/Actions/GameObject/GetChildNum.cs:33)
HutongGames.PlayMaker.FsmState.ActivateActions (Int32 startIndex) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:205)
HutongGames.PlayMaker.FsmState.OnEnter () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/FsmState.cs:175)
HutongGames.PlayMaker.Fsm.EnterState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2767)
HutongGames.PlayMaker.Fsm.SwitchState (HutongGames.PlayMaker.FsmState toState) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2714)
HutongGames.PlayMaker.Fsm.UpdateStateChanges () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2642)
HutongGames.PlayMaker.Fsm.UpdateState (HutongGames.PlayMaker.FsmState state) (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:2783)
HutongGames.PlayMaker.Fsm.Update () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/Classes/Fsm.cs:1994)
PlayMakerFSM.Update () (at C:/Projects/Playmaker_1.9.0/Projects/Playmaker.source.unity/Assets/PlayMaker/PlayMakerFSM.cs:579)

when I run the simple setup in the attached image.
Please check        crash get child num.jpg

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

using UnityEngine;

namespace HutongGames.PlayMaker.Actions
{
[ActionCategory(ActionCategory.GameObject)]
[Tooltip("Gets the Child of a GameObject by Index.\nE.g., O to get the first child. HINT: Use this with an integer variable to iterate through children.")]
public class GetChildNum : FsmStateAction
{
[RequiredField]
        [Tooltip("The GameObject to search.")]
public FsmOwnerDefault gameObject;

[RequiredField]
        [Tooltip("The index of the child to find.")]
public FsmInt childIndex;

[RequiredField]
[UIHint(UIHint.Variable)]
        [Tooltip("Store the child in a GameObject variable.")]
public FsmGameObject store;

public override void Reset()
{
gameObject = null;
childIndex = 0;
store = null;
}

public override void OnEnter()
{
store.Value = DoGetChildNum(Fsm.GetOwnerDefaultTarget(gameObject));

Finish();
}

GameObject DoGetChildNum(GameObject go)
{
return go == null ? null : go.transform.GetChild(childIndex.Value % go.transform.childCount).gameObject;
}
}
}

Please explain why this is happening.

I believe if there is no child index 0 (or no child found on whatever index is used) then this actions should just continue. No?
It simply does not save what has not been found into the variable and continues on with the code. Isn't that how it should work?

Br
szomaza

edit:
GetRandomChild action for example does not crash in this same situation. It just does not store the non existing child it did not find and continues.

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 GetRandomChild : FsmStateAction
{
[RequiredField]
public FsmOwnerDefault gameObject;
[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;

storeResult.Value = go.transform.GetChild(Random.Range(0, childCount)).gameObject;
}
}
}

« Last Edit: December 23, 2018, 01:29:35 AM by szomaza »

Alex Chouls

  • Administrator
  • Hero Member
  • *****
  • Posts: 3634
  • Official Playmaker Support
    • View Profile
    • LinkedIn
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #7 on: December 29, 2018, 12:24:45 AM »
Hi, looks like an oversight in that action.
I've attached a version with better error checking.
I'll include it in the next update.

Glad you're still having fun with Playmaker! :)


szomaza

  • Sr. Member
  • ****
  • Posts: 253
    • View Profile
Re: Not Storing - Get Child Num, Get Child, Find Child, Find Game Object
« Reply #8 on: December 29, 2018, 01:20:11 AM »
Thanks for the quick fix!
Works nicely.