Forums › 💬 NodeCanvas › ⚙️ Support › Need Help with a Custom Task sending Events to Multiple Objects
Hi Gavalakis,
I am trying to create a Task in NodeCanvas which has an input List of GameObjects and needs to send an Event to each one of those GameObjects. I can not use SendGlobalEvent because I have more than 50 GameObjects that use the same FlowSript Asset.
I am doing a ForEach to the list and want to send an event to each of those GameObjects.
I am stuck with that, I am getting errors that the “agent” is a read only
can you suggest what the best use of the API to accomplish this?
thanks in advance!
Hey,
Sure. Here is the Task 🙂
SendEventToObjects.cs
|
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 |
using UnityEngine; using System.Collections.Generic; using NodeCanvas.Framework; using ParadoxNotion.Design; namespace NodeCanvas.Tasks.Actions{ [Category("✫ Utility")] [Description("Send a Graph Event to multiple gameobjects which should have a GraphOwner component attached.")] public class SendEventToObjects : ActionTask{ [RequiredField] public BBParameter<List<GameObject>> targetObjects; [RequiredField] public BBParameter<string> eventName; protected override void OnExecute(){ foreach(var target in targetObjects.value){ var owner = target.GetComponent<GraphOwner>(); if (owner != null){ owner.SendEvent(eventName.value); } } EndAction(); } } [Category("✫ Utility")] [Description("Send a Graph Event with T parameter to multiple gameobjects which should have a GraphOwner component attached.")] public class SendEventToObjects<T> : ActionTask{ [RequiredField] public BBParameter<List<GameObject>> targetObjects; [RequiredField] public BBParameter<string> eventName; public BBParameter<T> eventValue; protected override void OnExecute(){ foreach(var target in targetObjects.value){ var owner = target.GetComponent<GraphOwner>(); if (owner != null){ owner.SendEvent<T>(eventName.value, eventValue.value); } } EndAction(); } } } |
I will include this task in the next release too, so feel free to place it under the “NodeCanvas/Tasks/Action/Utility” folder 🙂
Let me know if that is what you were after.
Cheers!
Hey,
It works Great!! Thanks a lot for that!!
just one thing… the regular SendEvent.cs task logs in the console when an event is sent and received, see attached screen. That is really useful
however this new Task does not.
Is it possible that you can add that log functionality?
Thanks again!
Hey,
You are welcome.
Events are logged with this task as well, but only as long as the gameobjects in the list do indeed have a GraphOwner component attached and as such the even is indeed send in the end, since for gameobjects without the GraphOwner attached in the list, the even is not send.
Can you please confirm that?
Thanks
🙂
Hi,
actually I just confirmed that the event is not sent, thats why is not logged
my scenarios is: FSM Nodecanvas sending events to Multiple Objects with FlowScripts,
thanks in advance!
Hey,
I just rechecked this and can confirm that it works here :/
I’ve also checked like you said, an FSM sending and event to multiple FlowScriptControllers.
Hmm.. Are the FlowScriptControllers enabled when the event is send? That is, is the “OnEnable” option set to “Enable Behaviour” in the FlowScriptController inspector and the gameobject active as well?
Can you share a bit more information on your FSM setup please?
Or better yet, if you can send me a small project reproducing this, that would be great of course!
Thanks in advance.
Hey,
sorry I was using one other variable with similar name and got confused,
This works perfect!!
