Jump to content
  • Advertisement
Sign in to follow this  

Coding a Simple Text Menu Interface

This topic is 2911 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 making a game that's a bit of an Alien Breed clone of sorts. As part of that, I'm now implementing a very similar weapon/item purchasing system much like in Alien Breed whereby the player activates a terminal in game and is presented with a simple text menu screen to scroll through and make selections for buying various game items etc.

I have activating the terminal sorted, but now it's onto actually coding the menu navigation itself. I've done mock ups of what I want in Photoshop and I'm pretty satisfied that those will be fine for what I need and know how the navigation and selection etc. should work.

My problem now is just in the specific approach to the coding itself, and avoiding a horribly inflexible, messy or overcomplicated way of getting it done that'll come back to haunt me soon after I get into it.

What I need is:
- A Main Menu where you're presented with a series of text options, selecting them by moving a caret up/down with the arrow keys. These would be primarily the Weapons page, Ammo page and Items page, as well as Exit.
- The Weapons page, where weapons would be listed (probably loaded from an XML file somehow, not important exactly how) thus:

<Gun Name>.........<Gun Desc.>.............<Gun Price>

Allowing scrolling through the weapons and on pressing Enter, displaying a Confirm Message somewhere towards the bottom of the screen and switching to moving between Yes or No options under that.
- The Ammo page which would be almost identical to the Weapons page, but instead of a simple Yes/No confirm message, the user would scroll through the desired quantity of ammo to purchase at a time (from 0 to whatever can be afforded).
- Other pages that would probably not differ much from the previous 2.

A few of the things that have given me pause for thought are:
1. How to represent each text selection, especially given that Main Menu options act very differently (changing the current page) to Weapon/Ammo options (going into a confirm option).
2. How to neatly handle the move from selecting from the list to restricting selection to the new confirmation options.
3. While the Ammo and Weapon pages would be very similar, how I should handle the differences between confirmation options after item selection.

With 1, I've wondered if some kind of class representing each menu choice would be good and then perhaps include the text and what it refers to. A problem with that is making a class that would work with both Main Menu choices and Item choices on other pages. Making some kind of base class and deriving 2 others from that seems like overcomplicating a feature that really just needs to function acceptably without much complication.

I could keep track of the position in the list of selections that the user is currently on, but then when the confirmation comes up, how should I neatly handle freezing the main list selection and switching control to the new mini-menu? I could think of some messy switch statements and the like but I can't really see a way that looks well organized to me.

How would you approach this? I'm not looking to create a very flexible system for future use that can be easily altered to accommodate most menu requirements for most games, just something that'll work for this but won't end up being confusing to code or hard to understand later, or that requires tedious changes to multiple locations just to change minor details.
I've never done something quite like this before, so while I'm getting a few ideas, I'm having trouble designing a cohesive system that would seem to neatly allow everything I need to implement without ugly hackish code.

Share this post

Link to post
Share on other sites
I just need a rough idea of where to begin in high-level terms. I'm not sure if there are a couple of places were specific kinds of classes might be very useful or if there's a more obvious solution.

Share this post

Link to post
Share on other sites
Although you wrote a very detailed question, I'm not sure I understood it, but I'll try to help anyways.

A decent data-structure for storing menues is recursive single/double-linked-list of class CMenuItem.
A CMenu class will hold the items

string name;
CMenu* child_menu_items; // if your menues have child items


CMenu {

list<CmenuItem> menuItems

The CMenuItem will hold all information releated to a menu item ( e.g string , checked status if needed, short cut key, etc. )
The CMenu will store information like current selected item, etc.

now drawing the menu is a bit trickey. alot of programmers I know, would place the a drawing function in the CMenu. But consider this scenrio. If you are now using a text display then you will draw it one way, and if in the future, you change to a graphic display you will need to change this function. what if you want both ? what if you use different OS ? So it would be better off, creating a drawing function in some other class (like application or display class that will draw the CMenu item)

Share this post

Link to post
Share on other sites
That's got me thinking now...

So if you were to use CMenuItem to create a main menu of something like:

Would the idea roughly be to have a CMenuItem for Weapons, and then each weapon choice that would be present in the Weapon page of the menu would be included in the child menu items?

Something like:
CMenuItem WeaponMenu( "Weapons" );
WeaponMenu.addChild( new CMenuItem( "9mm Handgun" ) );
WeaponMenu.addChild( new CMenuItem( "Shotgun" ) );

And so, after choosing Weapons, the next page would be constructed somehow by looking at WeaponMenu's child menu items?

In that case though, how should special cases be handled? Which is to say, like I described before, if Weapons or Ammo were to be displayed in lists slightly differently or had different actions after selecting them, how might that be organized?
I suppose you could have set "styles" or some such, which would determine the layout of the child menu items when activated, using a switch maybe, but maybe I'm getting messy again...

Share this post

Link to post
Share on other sites
If you have different menu items that have more information you can inherit from CMenuItem, e.g. CWeaponMenuItem.
But a word of advise regarding inheritance. It is a powerful tool, but should be used wisely.

If you find yourself creating 18 different Menu items, see if they a lot in common, maybe you should re-think the menu item class.
always look for common properties. if you are just changing the display maybe you should think how to store the string so you could display both menus in one function.

you could also create your menu items dynamically when and reading the data from some table (and not use hard coded menu items)

and don't call function from the menuitem select. for each item add an ID property, which will be unique.
once the item is selected, call an event like "on_menuitem_selected" and pass the ID
this way you can have whatever functionality you want for your items + if a parent menuitem is selected, you can build your child menu at that point.

hope it is clear enough.

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!