11. Main Menu

We're almost done with the tutorial! The last part we need to add is the main menu.

The main menu for our platformer is a level selector: it allows you to revisit any level you've unlocked, without having to start from level 1. It also allows you to start a new game, meaning it will re-lock every level except the first one, and to quit the application.

1. Level select buttons

Open the Menu scene. You'll notice that every button under Canvas / Panel / Buttons is actually a prefab. This prefab is located under Prefabs / MenuLevelButton

  1. Select the MenuLevelButton prefab in the project window
  2. Add a new string object variable called Scene 
  3. Add a new flow machine component
  4. Create a new macro for it called MenuLevelButton

Then, for each level button in the Menu scene, edit the Scene variable to match the name of the level:

  • On Level1Button, set it to Level1
  • On Level2Button, set it to Level2
  • On Level3Button, set it to Level3
  • On Level4Button, set it to Level4

We will use this variable to determine which scene to load.

1.1 Load level on click

In the new MenuLevelButton macro, add the following group to load the scene on click:

1.2 Enable if unlocked

Remember how, back when we created the level change script, we saved a LevelX_Unlocked variable upon level completion?

Now is the time to use it: we will check if a saved variable with the matching scene name exists and is true. If it does, the button will be interactable; otherwise, it won't:

1.3 Initial saved variable for new games

Up until now when we were testing, we started by playing the Level1 scene. But if the player started at the Menu scene with no level unlocked, they couldn't even choose the first level from the menu!

To fix this, we will create an initial saved variable. These automatically get created for new games, but will be overridden by any data in the saved game. All we need to do for this is declare the variable in the Initial subtab of the Saved variables tab:

This way, when opening the game for the very first time, Level1_Unlocked will be true.

2. New game button

The new game button will unlock level 1 and lock every level above 1. For this, we will use a For loop, which allows us to increment a number and do an operation on it at each step:


One important thing to note here is that the For loop is inclusive for the first number, but exclusive for the last number. This is why we wrote 5 instead of 4 in the Last field. The Step determines by how much the index changes at every iteration: in our case, it gets incremented by 1.

What this graph does, in other words, is:

  1. Set Level1_Unlocked to true
  2. Enter the for loop:
    1. With Index = 2, set Level2_Unlocked to false
    2. With Index = 3, set Level3_Unlocked to false
    3. With Index = 4, set Level4_Unlocked to false
  3. Exit the for loop

4. Quit button

Just like we did in the pause menu, add an embed flow machine to the QuitButton object with the following graph:


If you test your game now, you should have a pause menu that remembers which levels you unlocked:

This article was helpful for 9 people. Is this article helpful for you?

+1

I have a problem with 11.1 under Bolt 1.2.2. The object scene variable "Scene" as String is not saved at the prefab. I create the variable, but if I try to define it as type string, it is changed back to {null} immediately.

Anyone else has this problem?

Solved: Forget to set the prefab flow to macro. It was still as embedded.

+1

It happened to me too. The thing is... that prefab it has no flow at that moment of the tutorial. Setting it to another type (boolean for example), and then switching it to String it works.

Yes. Need to add a Flow Machine before you set the Scene string type. Just need to re-order the instructions above

Not sure what's happening, but it's not going into Level 3 after completing level 2. Also, when I go back to Menu it doesn't show any level is available except for level 1. 

Beyond this, I had trouble creating an object variable for the MenuLevelButton prefab. It wouldn't save the type of button for some reason. 

Hello!

I attempted to insert "Level1" - "Level4" into Scene (string) of MenuLevelButton.
However, the Sscene(string) of all buttons are the same character string.

> Then, for each level button in the Menu scene, edit the Scene variable to match the name of the level:
> On Level1Button, set it to Level1
> On Level2Button, set it to Level2
> On Level3Button, set it to Level3
> On Level4Button, set it to Level4

That is, if I insert "Level1" in the Scene(string) of Level1Button, the contents of Scene(string) of Level2Button - Level4Button will also be "Level1".
I also saw a video tutorial, but I could not operate it the same way.

Thank you.

PS.
I am using Google Translate.
I'm sorry if the sentences are weird.

Hey Everyone,

I'm currently having the same issue with changing the value of the "Scene" variable, and I can't figure out how to fix it.  I did the whole tutorial almost a year ago, and everything went super smooth.  Now, with updates to Bolt and Unity, I'm having a lot of issues.  Does anyone know of a resource, either written or video, that shows how to adapt the tutorial for the new changes?  It's taking me way longer to build the tutorial the second time around because of these issues.  Thanks!


+1

Love Pug,

I think I just figured this one out.  I went back to Section 4. - "Level Change" and looked at the way the Scene variables are handled there, then compared it to the Main Menu Scene variables.  The difference is that for some reason, on the Main Menu, when you click on the "Level1Button" object, then look at the object variable section, there is now a split between "prefab" and "Instance".  this is the big change.  You need to go into the "instance" section, then add a variable named "Scene", type String, then put "LevelX" in for the value.  Now you can actually click on Level 1 on the menu, and it will load.  I don't totally understand the changes that have been made, but this seems to work for me.

Hi! Mr.Ryan

The reply to you has been delayed. I'm sorry.

Based on your advice, I set a value for Instance instead of Prefab.
However, all the values are the same.

I will continue to test what is wrong with me.
Thank you!

I understood the cause.
After changing the value of the instance, it becomes apply.

I have a doubt.
Do not click Apply OK?