Hi
I’m new with Unreal engine and I don’t know how to use a global variable…
I just want to share a data(variable) between blueprints but I cant ,
so I’ve searched on a google got some tips and none of them worked…
please help me…
Create a GameInstance blueprint (for example, named “MyGI”). Then in Project settings > Maps & Modes, set GameInstance to be the one you just created.
Then on your GameInstance blueprint create the variables you want to be global. Then from any other blueprint call 'Get GameInstance" then pull out that pin and “cast to MyGI.” Then you can access your global variables and they will persist between level loads.
I do the same thing as Jared Therriault, except I use my game mode for most of my global variables. My game mode is called “MyShooter”, so anytime I want to get/set a global variable, I use a CastToMyShooter node with a GetGameMode attached, and then I get/set whichever variable/variables I want to change. This can be called from…I want to say anywhere that has a blueprint…ie. can be called from actors/pawns/widgets/HUD.
There are different ways to pass variables between blueprints, but I think this is the best way if you are new to UE4.
The game mode only exists on the server though.
Just read a bit on gamestate vs. gamemode, looks like you’re correct DjSt3rios…I think however, for a single player game it would not matter which you use (gamestate/gamemode) for variables. Does that sound right?
Yes, that is correct
But when both methods work same way (from implementing that variable functionality perspective) it is better to use “proper” way. Some day you may decide to add multiplayer, so why not stick to convention and design of engine from beginning. That also teaches correct behavior for future projects that may be multiplayer.
In my example I suggest GameInstance. The reason is just that GameInstance is never destroyed as long as the game persists so the variables are truly global. GameMode requires you to load variables from a save game when loading a new level. In that case you could forgo GameMode altogether and just use the SaveGame. Of course you never have to worry about that if you use level streaming or never load another level. In the end it really depends on your type of game and personal preference - but I prefer GameInstance
This is very true. UE4 has done this right. I make this a place to store a lot of variables that affect the game itself and I put everything neatly in their own categories. When it comes to objects having their own configurations…like what clothing items are being worn and items in inventory…etc. I use a save on them individually.
Format of the Save is => Name<UID>_Clothing Name<UID>_Inventory Name<UID>_Stats
UID is a random Int32 from 1 to 2,000,000,000 that every ‘Live’ object receives in their properties blueprint. This is so I do not have to rely upon the UE4 ID and can search IDs of objects based upon this ID which is easily grabbed in an array.
Hi Jared - I’ve tried this for a similar need and is working fine - but I need to reset that accumulated value (like Coins collected or no.of instances of an actor BP) to zero once I click a particular Event Button where I’m writing another logic that needs to restart the accumulation. If I do that reset in the same Actor BP where I’ve casted the BP_GameInstance, then it is resetting for every instance of BP_Actor which is not what I need.
How can we reset this variable once with a Keyboard Event?