NodeCanvas Forums › General Discussion › Bug in ButtonClicked Condition?
I got a FSM with 4 states, one being a sub FSM.
I use one button (“skip to next state”) which triggers through the state machine for debugging each state.
All works well in the first run.
The last state optionally returns to the first state and this is where it starts to fall appart … but I think I fixed everything.
Part of it was my code, but I also had to change the ButtonClicked condition, as something was wrong with the listener (was called twice in the second run).
Cand you confirm this?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 |
using NodeCanvas.Framework; using ParadoxNotion.Design; using UnityEngine.UI; using UnityEngine; namespace NodeCanvas.Tasks.Conditions{ [Category("UGUI")] public class ButtonClicked : ConditionTask { [RequiredField] public BBParameter<UnityEngine.UI.Button> button; protected override string info{ get {return string.Format("Button {0} Clicked", button.ToString());} } protected override string OnInit(){ //button.value.onClick.AddListener(OnClick); return null; } protected override bool OnCheck(){ return false; } void OnClick() { Debug.LogWarning("OnClick"); YieldReturn(true); } protected override void OnEnable() { base.OnEnable(); button.value.onClick.AddListener(OnClick); } protected override void OnDisable() { button.value.onClick.RemoveListener(OnClick); base.OnDisable(); } } } |
Hello,
Hmm. For the event to be raised twice, it means that the OnInit is also called twice, but OnInit should really only be called once in the code. I tried reproducing a case where it could be called more than once but (thankfully )didn’t really happen.
Do you by any chance, have the same UI button reference on more than one ‘ButtonClicked ‘ conditions which are one right after the other?
What I mean by that, is something like the following image. In this case, even a single click at the button (which is the same), will instantly make two transitions, since at the time the button is clicked, both of the conditions are true.
Let me know if that is indeed the case, or something completely different.
The change you’ve made by the way, is fine and should work fine as well. I am just trying to figure out what is the cause 🙂
Thanks.
Join us on Discord: https://discord.gg/97q2Rjh
Hi, yes, that is indeed the case.
So is this intended behavior or should I understand what’s going on behind the scenes?
Thanks!
Hello,
This is an expected behavior due to how events like this work, but some times it is also not the indented one as well. 🙂
For an easier alternative fix to your code, please open up ConditionTask.cs instead and change method YieldReturn line #71, to the following:
1 2 3 4 5 6 7 8 |
protected void YieldReturn(bool value){ if (isActive){ yieldReturn = value? 1 : 0; StartCoroutine(Flip()); } } |
Let me know if that works for you.
Thanks!
Join us on Discord: https://discord.gg/97q2Rjh