Sign in to follow this  
codemonkey423

Menu design for a text-based game?

Recommended Posts

codemonkey423    122
I've just been messing around lately and tried to create a simple text-based MUD in C++. It went pretty well, but one of the problems I kept running into was a menu hierarchy. I kept using case statements, which became confusing and hard to manage. Is there a better way to do this? The menu might look like, for example: 1. Inventory 2. Travel 2.1 Walk 2.2 Teleport 2.3 Examine 3. Skills etc... Something like that. Any ideas? Thanks. This has stumped me for a while. [Edited by - codemonkey423 on November 26, 2008 3:23:49 AM]

Share this post


Link to post
Share on other sites
Stani R    1070
How about a hash map that links menu items to some handler code? Presumably, the user has to type a number to select a menu, so just call something based on what was stored for that number. You could have one hash map, and restrict input by using different input validation methods, or you could have one map per menu.

Just something off the top of my head, not sure if it will really be less unwieldy. (Actually it might be worse than writing a switch statement...)

Share this post


Link to post
Share on other sites
PKLoki    1492
(Based on an old C++ console game that, unfortunately, I have since lost the code for...) My solution to this, which was appropriate for a space-trading game (think of Frontier only text-based) was to have my game logic generate a list of command objects that a player could choose from, depending on the current position/status of the player. Each command object has a string representation so listing a menu of options is just a case of iterating through and displaying them. Available options are defined by the location/state of the player - so for example if the player is in the spaceship shop at a given space station he might be provided with "buy new ship, upgrade, repair" ...etc command options.

One benefit of this approach is that AI 'players' in the universe can perform the same logic - they will have a similar situation and various options, so the AI code for an NPC just comes down to choosing the most appropriate option from a list. The difference, apart from obviously not printing their 'menu' to a screen, is that NPCs tend to be limited to more general commands like 'travel to x' rather than 'buy 2 tons of silicon at 30 credits apiece.' That was just to keep things simple.

The game logic code received all of the commands chosen by the various game characters (including the player) and then updated the state of the universe as appropriate.

As an extra complication I distinguished between 'real-time' and 'elapsed-time' commands. For example, if a player sells something at a space station the overall universe logic is not updated and the commands from AI characters are not processed. It's assumed that this is a real-time operation and nothing significant will change diring that time. However when a player travels from one star system to another (an 'elapsed-time' operation) the rest of the universe updates as well.

So in summary (and this may not be perfect but worked in my situation): separate the logic (model) from the console (view/controller) in such a way that the logic can behave well regardless of the views that are giving it commands. Try to make the player's console just a graphical representation of how any entity would interact with the system. Each view just posts a requested command to the logic, and the logic decides the world's behaviour in response to those commands. But finally, since a console game is not realtime you need to give the player a special status in the sense of determining the flow of 'time.'

Hope this helps, though I fear I may have rambled a bit..

Share this post


Link to post
Share on other sites

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