1
\$\begingroup\$

So I'm about to start development on a game in unity that is going to require multiple choice each choice having a some what unique function. I've thought of 2 ways of doing this but I'm unsure if there is an easier way as I've never really worked with unity.

So the first way I was thinking about doing this and I think better then the second would be to make a class for each choice with all the functions that belong to that choice in there and then just calling it when its needed. However, I don't know if this will scale very well as I will have over 100 different events with their own choices...

The second way I was thinking was to do it via JSON and then just code each function to parse that. So for example: addmoney, dothis, dothat etc... However this seems like a lot more work then it would be worth so I'm not 100% sure...

So I'm wondering what option would be better and if anyone else has any suggestions I'd love to hear!

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

Have a base abstract class of Choice. It has options such as "Choose". Then the functionality of choose is implemented in the subclasses.

public abstract class Choice
{
    void Choose();
}

public class RightChoice : Choice
{
    public void Choose()
    {
        Debug.Log("You made the right choice!");
    }
}

public class LeftChoice : Choice
{
    public void Choose()
    {
        Debug.Log("You made the left choice!");
    }
}

Then you can proceed to sub-class LeftChoice and RightChoice as needed.

Disclaimer: For the sample code above, an interface would be better. But I suspect that an abstract class will be better for your situation.

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

you have lots of choices. i list a few and you can choose which works best for you.

1) simple if.. else statement. its hard-coded and you dont have much freedom

2) switch case statement. its not OO and is not adviced.

3) using command pattern. this is like @Evorlor done in his answer. you can change command or choices by simply changing the class.

4) using delegate and events. you can simply Make a Delegate like Action and change choices just by set different function to it.

\$\endgroup\$

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