GameMode variables changed by the widget blueprint are at their default values

The above image shows the main menu, which is a blueprint widget. There are five variables that are set in the menu: game speed, block count, wall height, level count and a bool variable that tracks if the game mode is play mode or build mode. These variables are set with sliders, and when “Build Mode” or “Play Mode” buttons are pressed, these variables are supposed to set the corresponding variables inside the GameMode.

This is the “On Clicked” event of the play mode button. The event gets the values of the sliders, and uses the “SetGameModeValues()” BlueprintCallable function to set the game mode variables.

SnarisGameMode.h (1.6 KB)

This is the .h file of the parent class of my BP_SnarisGameMode

SnarisGameMode.cpp (686 Bytes)

This is the .cpp file of the parent class of BP_SnarisGameMode

Here is the problem:

The variables of the game mode class that were to be set to the values determined inside the main menu are not changed, and the default values set in the c++ constructor remain unchanged.

The above image shows the tick event of the BP_SnarisGameMode printing the “BlockCount” variable, and the value is the default value.

Also printing the result from the SnarisGameMode.cpp file’s tick function, the variables are unchanged.

How can I set the game mode variables to the values determined in the Main Menu widget blueprint?

GameMode is not persistent across levels. This means that anything you set in GameMode for specific level will be gone when changing levels, even if the two levels uses the same GameMode class.

To save your menu data into a persistent class I’d suggest using GameInstance. GameInstance is persistent from the second the game launches to the second it ends.

You can use the levels blueprint to set the values in your GameMode when the level loads.

You want different game modes for different maps. I highly recommend having a GameMode specifically for your menu map, and a GameMode specifically for your game maps.

So, save your data to your GameInstance when pressing play, and load them from GameInstance into your GameMode when the level loads.

Thank you for the explanation, everything works as intended now.