0
\$\begingroup\$

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.

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

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) };

//in your caller method
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 ;)

\$\endgroup\$
0
\$\begingroup\$

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?

Switch :

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?

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.