NodeCanvas Forums › Support › 'Check CSharp Event' trouble › Reply To: 'Check CSharp Event' trouble
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