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)…
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).
Click Add New in your content browser and choose Blueprint Class
Open All Classes and choose Game Instance. Name it something appropriate. I am using MyGameInstance in this example.
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):
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!
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?
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.
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.
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?
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.”