Sign in to follow this  

Menu Question

This topic is 4283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm currently in the process of throwing together a menu system for our current project and was wondering what the best way to implement a menu system would be. The easiest method I see right now is to break the game down into various states, and let the menu decide what to do based on what state the game is in. However, this is a rather ugly solution, requiring state checks every frame, resulting in large if/else or switch statements that bog down the code. I was wondering if there was a better alternative to this, or how you guys have implemented menus in the past.

Share this post


Link to post
Share on other sites
Quote:
Original post by ETFBlade
I'm currently in the process of throwing together a menu system for our current project and was wondering what the best way to implement a menu system would be. The easiest method I see right now is to break the game down into various states, and let the menu decide what to do based on what state the game is in. However, this is a rather ugly solution, requiring state checks every frame, resulting in large if/else or switch statements that bog down the code. I was wondering if there was a better alternative to this, or how you guys have implemented menus in the past.


What I would recommend doing is implementing the Observer pattern between the menu and the game logic (i.e. whatever handles the game states). Under this pattern, the menu (as the observer) would be told to update itself whenever the game state changes. Then I would have a mapping between the available game states and different menu "views". That way, you avoid long conditional statements by simply getting the view mapped to the given game state. Here is some Java code to better explain things:


public class GameLogic extends Observable {

public static enum GameState {
// game states
}

private GameState state;

// ...

public void changeState(GameState state) {
this.state = state;
setChanged();
notifyObservers(state);
}

}

public class GameUI implements Observer {

private HashMap<GameState, GameMenu> menuViews;

private GameMenu currentView;

// ...

public void update(Observable o, Object arg) {
if(o instanceof GameLogic.GameState) {
currentView = menuViews.get(o);
}
// do other things, perhaps
}

}


Then, all you need to do to tie the two things together is:
gameLogic.addObserver(gameUI);


Hope this helps. [smile]

Share this post


Link to post
Share on other sites

This topic is 4283 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this