Building a Menu ground up. Part 2.

Welcome back!

Last time we went through everything that was needed to build the buttons we need for the menu, and since they’re completely self-managing we dont need to do anything except check if the button returns true or false when we update it.

What we need to do is some kind of structure that decides what buttons we currently can look at, what buttons we can interact with, and some way of knowing what will happen when they are pressed.

So for the issue of only showing a set combination of buttons depending on where in the menu you are positioned I figured using states is a good way to go. And since our state machine will keep updating the menustate until we return false as a signal to change state we are able to have a switch statement in the update function of the menustate.

As our buttons will give off sounds when we hover over them, we only want to update the buttons that are relevant for this state of the menu, so we will iterate through our container of buttons, and if we find an button that matches the type of button we want active in our state we proceed by updating it. If it returns false we keep on iterating through the container, and when we reach the end we are done with this update of the menustate. However, if the button returns true, we first need to know what type the button is, so that we can figure out what to actually do now that the button has been pressed.

Things we might want the button to do include:
Changing State of the menu – easily done by changing the value of the state variable in the menustate.
Starting the game – If we return false in our update loop the engine will look for the next state to be run, and since the menustate never exits to a state other than the gamestate we can have that instruction be static.Exiting the game – The state has access to our system struct which contains isRunning, which will cleanly turn off the engine and exit the game cleanly.
Changing the volume of the game – SFML has implemented an Listener class, which can be accessed from anywhere. One of the functions of the listener is SetGlobalVolume, which as you might expect sets the volume of the rest of the game.
Show the highscores – Since we dont want to read from the highscore text file every time we update the state if we’re in that state, we will read the scores when we enter menustate and remember them until we leave. When this is done the rest is easily solved.

That’s the end of my guide of how to make a menu for your game! I hope this was helpful and that you will not have any problems when it is time for you to make one.

I will not see you next week, as this is the end of our project. I might write more in the future, but for now there will be no more posts until I am required to write more.

I wish my readers all the happiness in the future, and I might see you in the future! For the time I will leave you with what René Descartes could’ve written had he lived in our age.

I_Think_Therefore_I_Am.png

That is all
Max

 

 

 

About Maximilian Höglund

2015 Programming