x

Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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

Product Version: UE 4.14
Tags:
more ▼

asked Jan 14 '17 at 10:18 AM in Blueprint Scripting

avatar image

edufissure
8 2 5 7

avatar image Fjellgnu Jan 15 '17 at 02:05 PM

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).

avatar image edufissure Jan 15 '17 at 03:08 PM

Would be great because im newbie in unreal ....

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

-1 answers: sort voted first

Hello edufissure!

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

alt text

  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:

alt text

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:

alt text

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):

alt text

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!

alt text

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

more ▼

answered Jan 16 '17 at 10:26 AM

avatar image

Fjellgnu
577 18 24 41

avatar image Carluw Jan 16 '17 at 11:09 AM

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"

avatar image Fjellgnu Jan 16 '17 at 11:56 AM

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.

avatar image edufissure Jan 18 '17 at 09:14 AM

Thanks a lot id try tonight...

avatar image AngiSv Feb 03 '17 at 04:28 PM

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?

avatar image Fjellgnu Feb 03 '17 at 08:02 PM

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.

avatar image AngiSv Feb 07 '17 at 10:43 AM

Hello Fjellgnu, sorry for the late reply!

alt text

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)

alt text

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.

avatar image Fjellgnu Feb 07 '17 at 11:37 AM

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: alt text

And here is my version of the widget: alt text

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

levelbp.png (145.2 kB)
widgetbp.png (98.0 kB)
avatar image AngiSv Feb 07 '17 at 09:36 PM

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

avatar image Fjellgnu Feb 08 '17 at 03:36 AM

No problem!

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

avatar image Nacho Royale Jan 15 '18 at 08:03 PM

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.

avatar image JesterDoobie420 Jan 15 '18 at 10:27 PM

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/. **WILL**as 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."

avatar image Nacho Royale Jan 17 '18 at 02:44 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question