Pass variable between main menu and levels

Hello i need help passing variables… i dont know how:

1- I have a main menu widget, where i can select the level of difficulty ( hard, medium, easy)… and i want to pass it to my character or enemies in the level played ( have more speed, or anything that could modify the difficulty).

2 - I need an easy way to save health, ammo and bombs and pass to the next level. Ex if i only have 3 ammo left, i begin the second level with 3 ammo left, not plenty of them ( the same happens with health and bombs) … When i open a new level i begin with initial quantity of them ( ex: 10 ammo, 3 bombs and 100% health)…

Thanks in advance

5 Likes

Hello edufissure!

You can use the Game Instance!

Create a game instance blueprint class, select it in the project settings under Maps and Modes - Game Instance, cast to it from your main menu level and store your values there. Then you can repeat the casting in every level you want and retrieve the information you need.

EDIT: I don’t have much time right now, but I can give a more detailed answer to this tomorrow (monday).

1 Like

Would be great because im newbie in unreal …

Hello edufissure!

I said yesterday you can use the Game Instance and this is how you do that:

  1. Click Add New in your content browser and choose Blueprint Class
  2. Open All Classes and choose Game Instance. Name it something appropriate. I am using MyGameInstance in this example.
  3. Add variables that represent the information you need to store. I have put in three examples (InsDifficulty, InsHealth and InsAmmo) but you can create whatever variables you need.

A very important thing to remember after you create your game instance is to tell the engine that you want to use this particular instance. You do that in Project Settins (Project Settings in the Edit menu). Choose Maps and Modes and you can change the instance here:

In your menu widget you need to store the information in the game instance variables before you load the first level. You can do this by casting to the Game Instance and send the information to it. It the following example I do this when the player clicks the play button:

To get to the Instance variables drag a wire from the blue pin on the cast node (As MyGameInstance) and open Variables in the menu. You should have access to both Get and Set versions there under the Default category (Context Sensitive must be switched on):

122177-gameinstancemenu.png

The final step is in your level (either level blueprint or regular blueprint class). Repeat the casting and Get the variables from the Game Instance. And remember to create the variables you need in your level or class blueprint!

Hope this helps!
Ask if you have questions or trouble.

12 Likes

Awesome answer!

but does the dificulty level modify health and other variabless stats? In that case, create event inside “game instance” that changes the variables by the “difficulty leve variable”

No, there is no game logic linked to any of the variables. They don’t do or modify anything. This is just an example on how to store information in the Game Instance.

Thanks a lot id try tonight…

hello, i’ve tried it like that with my game, where i’m saving scorepoints in the level blueprint to the game instance and then get the variables from the game instance into my widget blueprint to update the score shown in the hud. however it doenst update the number, but the variables ive defined are set correctly. do i need an event to update the hud?

Hello AngiSv

From the way you describe it, it sounds like it should work. If you could provide a screenshot of the widget blueprint it will be easier to see what is going on.

Hello Fjellgnu, sorry for the late reply!

Here I have the level blueprint, where I’m adding an event to my triggerbox to detect my ball going in the goal. it does increase the variable correctly, since I see it get printed on the screen. “Red Score Saved” is the variable in the game instance, while “red Score” is the variable in the level blueprint, and “red Score Value” is the variable in the widget blueprint for my HUD (sorry if that’s confusing)

Here I have the binding function for the variable “Red Score Value” in my HUD that should display the score(s). For some reason it doesn’t update in the HUD and stays zero even though I can see the variable changing when printing in the onActorBeginOverlap Event at the end.

thanks for replying and helping me, I have no clue what to do.

Hmm. The print string you’ve set up in the HUD, does that show the correct value? If it does you may have the wrong value hooked up to the return node. That value comes from the game mode and not the game instance.

Try to hook the ToText and Return node to the end of Then 0 execution line.

EDIT:
I have tried to set up something similar myself. It as simple as it can be with just a trigger box and and a widget to show the score.

Here is my version of the level blueprint:

And here is my version of the widget:

This setup worked and it showed the correct values in widget. Use this when you debug your own project.

Ahh yes that was the problem, taking the game mode instead of the game instance :smiley: I fried my brain over this for a week, thank you so much for your help! :slight_smile:

No problem!

You know, I would recommend disconnecting unsused nodes. It makes the blueprint easier to debug! Just a friendly advice …

I Have a question about the Game Instance in the default game mode…

We create a new blueprint for the Game Instance and end up replacing the default one. Does this mean we now have a Blueprint in the content browser named "New game instance’ (or whatever)… And if so… why did the default not have one… or if it did how do you access it or bring it into the content broswer?

I noticed the same thing when making my own player controller.

What other blueprints are not visible in content browser by default?

Thanks.

Afaik anything that is still default in Project Settings like GameInstance has such an “invisible” BP. They aren’t hidden, they’re just in a “top-level” folder that UE4 doesn’t show by default. Content Browser doesn’t know that anything “above” the “Content” folder for the open project exists so it doesn’t show it. Those files are in a folder somewhere in your install files, not advised to change them unless you really know what you are doing. **IE=**DriveLetter:/Unreal_Install_Location/Installation_Files/. doesn’t show, but DriveLetter:/Unreal_Install_Location/UnrealProjects/Current_Project/Content/. WILLas long as it’s imported correctly and/or in the correct format. UE4 is a bit weird about folder depth(folder in a folder in a folder…) and folder naming conventions, as well as automagically importing files dumped into …Current_Project/Content folders. Sometimes it does but sometimes it don’t and idk how to tell the diff or if it actually even matters to the finished product how one gets stuff into the Editor. I’ve had it notice texture files I just dumped into a …/Content folder and ask me what to do with them but not always, and moving .Uasset files between projects it doesn’t always do this either. I’ve had to restart the Editor entirely a few times to get it to see .Uasset files I moved, but not always, since they’re already UE4 format they are NOT “Importable” and there’s no “Add random files to Project” button, just “Import.”

Thanks for explaining that. Yeah I had it recognize files I didn’t actually want in there just because I moved them into a folder before.

Mostly I was just wondering what other blueprints i might not be aware of yet.
Player Controllers and Game Instances seem to be fairly important or used often.

or… things I might want to consider making from scratch.

I guess I will figure it out as the needs arise.

I dunno this logic, its looks very unusable. I cant use 1 Game Instance, i need more and more classes for different logics.

Oh Man, this indeed was what I was looking for