NodeCanvas Forums › Support › 'Check CSharp Event' trouble
Hi ! I’m honestly lost in the use of a simple Condition Box in a Behaviour Tree : the Check CSharp Event. I want to use it for a more complex stuff in our game, but since I can’t make it work, I resume it to a easiest use… and it still doesn’t work. Any help would be useful, I feel like I’m doing something wrong but where ?
Here’s a class Test that launches the System Action<float> :
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 |
public class CharacterCard() : MonoBehaviour { public event System.Action<float> OnTestAction; void Update() { if(Input.GetKeyUp(KeyCode.A)) { if (OnTestAction != null) { OnTestAction(1.0f); } } } void Awake() { OnTestAction += DoFloat; } void DoFloat(float value) { Debug.Log("[DoFloat] value is " + value); } } |
I have this class on a gameobject on my scene. When I hit the A key, the debug log displays into the console as planned. Perfect.
I’ve also got another gameobject with the following behaviour tree :
An here’s my problem : I’m only doing a simple set value when the Check Csharp condition is true. But it doesn’t work, the tree never launches that right side. Here’s the condition box in details :
Thank you very much for any help, I know it’s a very easy use of the nodeCanvas plugin but even that I can’t make it work.
Hello,
I just tried your code and setup to confirm if there is a bug or not, and honestly everything is working fine.
As soon as I press “A”, the Sequencer continues to the next node as expected.
Have you tried using a Wait action instead of SetFloat for example just to easier confirm that the condition is (or is not) evaluated?
Are there any warnings or errors in the console?
Let me know.
Thanks in advance.
Join us on Discord: https://discord.gg/97q2Rjh
Hi Gavalakis and thank you for your reply. I tested again in a clean project, and it was my mistake : I was drag&droping the BT at runtime, so, of course, it didn’t work. Silly, silly me v_v
I have another question thought, since I’m still trying to make this csharp event working for my project.
When the event is raised in the Condition Task, is it possible to compare this eventValue variable with a value of my own ?
I create my very own “Check CSharp Event” condition box, and I’m changing the code in Raised method, to make the return yield true launched only when the eventValue is equal to another value. Like this :
I want into my “Check Csharp Event” to compare this GlobalBlackBoard Variable (with the name of abigailCard) with my eventValue raised. I can’t make it work, and it’s quite an important aspect of my code :/
It’s probably a very simple task. I’m open to ideas or better methods.
Thank you !
Hello again,
Thanks for the follow up.
Regarding checking the value of the event directly, here is how I would do it.
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 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 |
[Category("✫ Script Control/Common")] [Description("Will subscribe to a public event of Action<T> type and return true when the event is raised and it's value is equal to provided value as well.\n(eg public event System.Action<T> [name])")] public class CheckCSharpEventValue<T> : ConditionTask { [SerializeField] private System.Type targetType = null; [SerializeField] private string eventName = null; [SerializeField] private BBParameter<T> checkValue = null; public override Type agentType{ get {return targetType ?? typeof(Transform);} } protected override string info{ get { if (string.IsNullOrEmpty(eventName)){ return "No Event Selected"; } return string.Format("'{0}' Raised", eventName); } } protected override string OnInit(){ if (eventName == null){ return "No Event Selected"; } var eventInfo = agentType.RTGetEvent(eventName); if (eventInfo == null){ return "Event was not found"; } var methodInfo = this.GetType().RTGetMethod("Raised"); var handler = methodInfo.RTCreateDelegate(eventInfo.EventHandlerType, this); eventInfo.AddEventHandler(agent, handler); return null; } public void Raised(T eventValue){ if (Equals(checkValue.value, eventValue)){ YieldReturn(true); } } protected override bool OnCheck(){ return false; } //////////////////////////////////////// ///////////GUI AND EDITOR STUFF///////// //////////////////////////////////////// #if UNITY_EDITOR protected override void OnTaskInspectorGUI(){ if (!Application.isPlaying && GUILayout.Button("Select Event")){ Action<EventInfo> Selected = (e)=> { targetType = e.DeclaringType; eventName = e.Name; }; var menu = new UnityEditor.GenericMenu(); if (agent != null){ foreach(var comp in agent.GetComponents(typeof(Component)).Where(c => c.hideFlags == 0) ){ menu = EditorUtils.GetEventSelectionMenu(comp.GetType(), typeof(T), Selected, menu); } menu.AddSeparator("/"); } foreach (var t in UserTypePrefs.GetPreferedTypesList(typeof(Component))){ menu = EditorUtils.GetEventSelectionMenu(t, typeof(T), Selected, menu); } if ( NodeCanvas.Editor.NCPrefs.useBrowser){ menu.ShowAsBrowser("Select Event", this.GetType()); } else { menu.ShowAsContext(); } Event.current.Use(); } if (targetType != null){ GUILayout.BeginVertical("box"); UnityEditor.EditorGUILayout.LabelField("Selected Type", agentType.FriendlyName()); UnityEditor.EditorGUILayout.LabelField("Selected Event", eventName); GUILayout.EndVertical(); EditorUtils.BBParameterField("Check Value", checkValue); } } #endif } |
Is this what you were indeed after?
Let me know.
Join us on Discord: https://discord.gg/97q2Rjh
I can’t believe all I was missing was that line EditorUtils.BBParameterField("Check Value", checkValue);
-____-” I didn’t understand why I couldn’t see my check value. It works pretty well 😀
Thank you a thousand times Gavalakis for your time, your patience and your awesome plugin \o/