Let’s Make: Traffic Department 2192 – Part 20

Prototyping - Main Menu Programming

Part writeup and code snippets coming soon.


We'll be bringing in three new objects and a couple scripts to implement our design from last time.

Concept Object Notes
Main Menu Controller obj_mainmenu Provides the 'gateway' through the game. Displays player interactions with the game menu.
Loading and Saving obj_loadsave Manages player interaction with the loading and saving screen. Includes both display and functional elements.
Popups obj_mainpopup Provides a blocking notification of player intent.

GML Code

We'll implement our three objects and alter the keyboard object in GML.

obj_mainmenu: Create 1

We'll continue to register the keyboard with most objects.


obj_mainmenu: Create 2

We'll define a state for each of the menu options


obj_mainmenu: Create 3

We'll manage several variables in the menu, including hardcoding pixel positions for menu options. Normally we wouldn't do this except we know the menu graphic will never change.


obj_mainmenu: Room Start 1

We'll usually grab other object references when the room starts because we can't guarantee creation order until this time.


obj_mainmenu: Room Start 2

Not all menu options will be selectable all the time. This is the initial state.


obj_mainmenu: Destroy 1

Deregister the keyboard


obj_mainmenu: Step 1

Here we'll handle the keyboard commands passed to the object stack. Direction keys move the selector and enter or return makes the selection.


obj_mainmenu: Step 2

Controls the fading, which ultimate changes the room depending on the menu selection.


obj_mainmenu: Draw 1

Render the main menu in layers. Draw the backlight then overlay the sprite.


Script: is_main_active

This script performs a series of checks to see if the menu is actually. Really we just need to return false if the popup is on the screen.


Script: create_popup

Sometimes selecting an option creates a popup. This script makes that happen.


obj_loadsave: Create 1

Register the keyboard and set refernece objects for interactions.


obj_loadsave: Create 2

Set the variables we'll use. The most important one is the mode that we use to determine whether to save or load.


obj_loadsave: Destroy 1

Deregister the keyboard


obj_loadsave: Step 1

Handle the keyboard commands. Like the menu, we change selection with the arrows and press enter to make the save/load choice. We'll directly save to the file here which normally I wouldn't recommend. But since this is the only interface to saving (no random autosaves,etc) then it really doesn't matter. Lazy speedcoding FTW.


obj_loadsave: Step 2

Fading that looks an awful lot like every other fading routine we've seen. I already wish GameMaker supported multiple inheretence so we could snap in code modules. I won't rail too hard against the engine though. I'm not a big fan of OO design in the first place.


obj_loadsave: Draw 1

Draw all of our save slots. I'm actually going to draw from the bottom up since I have a header. This makes for a very hack slot calculation as you'll see in the code.


obj_mainpopup: Create 1

Geting tired of registering the keyboard yet?


obj_mainpopup: Create 2

The popup will need some variables. The most important is probably the option value which determines which message to show. The option number directly ties to the hardcoded y-position of the sprites in the menu sprite sheet.


obj_mainpopup: Destroy 1

Deregister the keyboard


obj_mainpopup: Step 1

We'll need to handle the Yes and No options to leave the popup



obj_mainpopup: Draw 1

We'll draw slight fading over the menu and center the banner.


obj_keyboard: Begin Step 1

We have some buttons to add to our keyboard handler.