How do I cleanly integrate Nifty into my application?

I am using XML files and controllers in order to create and interact with my menus.

What’s the proper way to give my controllers access to game data or let the rest of my application know what action the user took? The way I’m currently doing it is by passing a singleton class around. The singleton class contains the game’s state and passes messages along.

Example:

  • Main class initializes the title screen menu.
  • The title screen is constructed using XML and is attached to a TitleScreenController
  • User selects the single player option
  • the onClick() in the XML points to the singlePlayer() function inside TitleScreenController
  • singlePlayer() attempts to initialize the singleton if it has not already been initialized.
  • passes a message to the singleton gamestate.addMessage(“title screen”, “single player”);
  • We are now back in the main class and in the update loop.
  • We check for messages in the singleton class gameState.getNextMessage();
  • Perform actions based on the message.

Is there a better way? I feel like I may be missing something and that there is a standard practice when interacting with Nifty that I’m not following.