Is it possible to store delegates with assigned variables in Lists?

+2 votes
asked Mar 24, 2016 in Programming by Skurniekto Student (370 points)
Hello, im working on improving my event system and i stumbled against a problem. I have searched everywhere but i cant seem to find any info. My problem is: I have an AIEvents class which has a ExecuteEvent method in it which subscribes to the OnEvent.AtEvent event, it takes one int parameter. Then i have another class called OnEvent which invokes AtEvent when something happens. My problem is: i want want to have a list of ExecuteEvent with a defined value so when its invoked it executes the ExecuteEvent method with the assigned parameter. Basically i want the ExecuteEvent method to know which OnEvent invokes it and this looks like the best approach to me. If you have any other approaches please share them. Thanks. :)

Edit: I forgot to mention that i cant store the ID (int parameter of ExecuteEvent) in OnEvent.
commented Mar 24, 2016 by JD Student (300 points)
You could use an anonymous inline delegate to register the event.
Edit: Or do I miss something?
commented Mar 24, 2016 by Gleny Student (480 points)
Without seeing any code - explaining the problem - it's hard to understand what's actually the problem is. :)

1 Answer

+3 votes
answered Mar 24, 2016 by Bugstacker Senior (4,800 points)
selected Mar 24, 2016 by Skurniekto
Best answer

You could design your own event class as following:

    public class EventArgumentPair<T>
        public Action<T> Event;
        public T Argument;

        public EventArgumentPair(Action<T> evt, T arg)
            this.Event = evt;
            this.Argument = arg;

    public class EventList<T>
        private List<EventArgumentPair<T>> delegates = new List<EventArgumentPair<T>>();

        public void Register(Action<T> evt, T arg)
            delegates.Add(new EventArgumentPair<T>(evt, arg));

        public void CallEvents()
            foreach (EventArgumentPair<T> del in delegates)
                // Execute event


This way you have the arguments linked to the specified events/delegates.

Use the class as shown below:

        static EventList<int> events = new EventList<int>();

        static void Main(string[] args)
            events.Register(Print, 7);
            events.Register(Print, 55);
            events.Register(Print, 61);



        static void Print(int i)
            Console.WriteLine("Called: " + i);


The code above outputs the following:

Called: 7

Called: 55

Called: 61


Let me know if that was your goal.


commented Mar 24, 2016 by JD Student (300 points)
OK... I admit that's better than some cheap inline delegates.
commented Mar 24, 2016 by Skurniekto Student (370 points)
Thanks for the fast answer! And yeah, this is exactly what i was looking for. I dont know why i didnt think of that. My system is actually designed very similiarly so i think it will be easy to rework. Have a nice day! :)
commented Mar 24, 2016 by Skurniekto Student (370 points)
One more question, is it possible to store delegates with assigned parameters at all?
commented Mar 25, 2016 by ismix Senior (1,150 points)
No. The delegate is just a variable, or more precisely: a pointer to a function. So there's no relation between the actual implementation and it's input/data.
Welcome to Bugstacker Q&A, where you can ask questions concerning Game Development, Programming and Design and receive answers from other members of the community.

Help for