3
\$\begingroup\$

When I say states I mean like what you would call scenes in unity. As in each phase of the game. But in straight up code for a console application in C#.

Example: Name select. Race Select. Store. Help Screen.

I've tried organizing each phase by methods, and while loops, but i've run into a lot of trouble these ways. How should I do it?

\$\endgroup\$
6
\$\begingroup\$

Take a look at the game state pattern.

The basic idea is that you have a class for every state. All classes inherit a interface that ensures basic functionality like entering the state, leaving the state, updating and rendering. Each specific state implementation holds (or has references to) all necessary resources and performs what you want to do. At some point in your program you keep the current state and provide methods for changing it, which are accessible by all states. Depending on your use-case you might want to have multiple states active, but for starters a single one is usually sufficient.

You can find a good in-depth explanation on this blog entry, but searches for "game state" will bring up many good results.

\$\endgroup\$
  • 1
    \$\begingroup\$ Fancy OOP patterns aren't necessary (and might over-complicating things for a newbie), but "game state" or "state machine" are definitely the keywords to search for. There are a lot of ways to implement this, so just find one you like and have at it. \$\endgroup\$ – Dan Sep 4 '15 at 18:34
  • 1
    \$\begingroup\$ @Dan As I interpret the OP, the important part is that they need to abstract, encapsulate and re-use code for presenting the interface and handling user input. I agree that an OOP pattern isn't necessary, but it seems like the crux is successfully abstracting interface tasks to make re-usable, configurable code. Even if a structured OO approach is beyond what OP can do for now, the task of abstracting the various interfaces into an inheritance model could be a light-bulb moment. \$\endgroup\$ – abathur Sep 4 '15 at 19:01
  • 1
    \$\begingroup\$ @abathur I did not intend to dissuade a motivated individual from exploring all of the options, including abstract inheritance models, but merely meant to inform that there are many ways to achieve his goal. While there is nothing wrong with your answer, there are a lot of articles and guides out on the web that hint at inheritance being the cure-all solution to every programming problem. The sooner the OP learns to see through this and make informed decisions about which design pattern is right for the job, the sooner he will find success. \$\endgroup\$ – Dan Sep 4 '15 at 20:12
  • 1
    \$\begingroup\$ @Dan certainly! Everything in moderation. :) \$\endgroup\$ – abathur Sep 4 '15 at 20:16
  • 1
    \$\begingroup\$ @Dan thanks guys. Keyword state machine really helped out. Really appreciate it a lot. \$\endgroup\$ – Chaz Carey Sep 5 '15 at 18:34

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.