# using an int to control events

Let me explain -- I'm just looking for a way that I can link a number to a set of methods. I want to randomly generate a "difficulty" int, and pick out an event based on the number I get. IE, if I get an int of 3 I want that to point to a specific event. What's the best way to accomplish this? I was thinking of story the events in lists, but methods can't be in lists. I thought about making the methods into delegates and putting those in lists but that seems unnecessarily complicated. Also, in the future I think I'd want to put multiple events at each level which would sort of make putting them into array or lists difficult.

• Delegates are not that complicated. You might want to take another look. Multiple options at each level just means a list of lists. – MichaelHouse May 25 '15 at 6:21
• I can suggest also a dictionary of delegates : stackoverflow.com/questions/11503887/… – dnk drone.vs.drones May 25 '15 at 7:01
• Does Unity accept delegates on a array as component? If so, this could be used. – MVCDS May 25 '15 at 17:56

Provided all the events have the same signature:

delegate return_type Event(parameter_list);
Event[] events = { new Event(Event1),
new Event(Event2),
//.....
new Event(EventN) };

events[random_int](parameters);


Delegates are the fastest and easiest method to accomplish what you want. And if you add more events, just add them to the array. It shouldn't be too difficult ;)

So you want to generate a random integer, and use the number generated to choose between a set of events. Seems like the best thing you could use would be switch case?

int caseSwitch = X; //your random generated integer

switch (caseSwitch)
{
case 1:       //if it's equal to 1 then
Console.WriteLine("Case 1");
break;
case 2:         //if it's equal to 2 then
Console.WriteLine("Case 2");
break;
}


Is this what you're looking for?