Dynamically adding and removing multiple events for multiple waypoints on runtime



  • Hello again

    I'm adding/removing a whole bunch of events individually as well as in groups.
    As of now I'm inserting all events in an array then remove them all when they need to be disposed for a whole new set of events. Is there a more efficient way to do this?

    private List<UnityEvent> ues = new List<UnityEvent>();
    
    foreach (UnityEvent ue in ues)
                        {
                            ue.RemoveAllListeners();
                        }
    
    int a = 0;
    foreach (WorldTile tempWT in wts)
    {
       UnityEvent tempUE = sm.events[a];
       ues.Add(tempUE);
       tempUE.AddListener(delegate { ActionForWaypoints(tempWT); });
       a++;
    }
            
    


  • UnityEvents are not that convenient to use programmatically. I remember having undesired outcomes with keeping listeners active on an event, even if you initialize a new List afterwards. So it is indeed required to call "RemoveAllListeners" on each event to wipe it.

    What do you mean by "more efficient"? If it works, it works ;)



  • Hmmm I'm completely new to Unity and this is my first time using UnityEvents but so far it all works actually. I have set up multiple lists of events and certain waypoints are bind to a several events etc but still adding and removing dynamically on runtime has not been an issue.

    I was asking because I saw this thread the other day - /topic/2437
    and you mention how you are going to update/enhance the event feature so I wondered if anything good has come out of it yet :p

    Thanks



  • What is mentioned in the other thread is just adding some additional C# delegates (3) to the movement scripts, so it is easier to react onto specific movement changes via code. Currently it is not high priority to release an update for this only though.


Log in to reply