Accessing Game Instance Variables via other BPs

Hello,

I am relatively new to using Unreal as an editor, I have been using blue prints, and I seem to be running into an issue when it comes to accessing variables.

I have a Blue Print interface that I use for my in world computer, one of the quests I have is to search for something on the computer. I have all this working, once the right search term has been used i currently have it set up to store true in the game instance, this is so that if the player returns to the computer to use this keyword again it will skip the content for that quest.

The searching, setting etc is all fine and this works great when accessing the tablet in another level. However it seems that once I have set this variable in the game instance, unless its a player/actor i cannot access this variable at all (from what i could get by poking around).

The quest system for reference is: https://youtu.be/vYL1cjOZKGQ?si=Gska3d7Oigu22fLK

it uses Blue Prints Class and Child Classes to store quests and their objectives. His examples quests are of arriving in a location or switching on a lamp, i thought i could use the code for switching on the lamp but use this global variable instead but it seems when i cast to game instance, it complains about needing an object, and i cannot provide it anything it needs as its not attached to the player or a level directly.

So my question is how do you make a child BP Class access and read the game instance data and if not what can it access and how do i save data there?

Any ideas or help would be appricated.

Regards
Bobby

Hello, and welcome to the forum.
To access the GameInstance which you configure inside the Project Maps&Modes panel, you call the GameInstance usually on Event BeginPlay and also set a reference for it.


A child BP / Blueprint can access the GameInstance by calling it, i.e. Cast to MyGameInstance then get VariableX value.

The GameInstance is usually not a child BP and you just create one for your project. The GameInstance can be accessed by all Players or Actors, for instance to get a variable about the current time of the day.

To save data for later use (on game restart) you need to create a savegame and setup a structure inside the SaveGame BP to store your custom values. To temp save data you can store it in the GameMode, which is assigned to the current Map (and lasts for the Map) or you store it on your Player Controller for instance.

There are good written posts and probably videos too on the basics of GameInstances (Lasts entire Game Session) & GameMode (Lasts duration of current Map), or for handling SaveGames (permanent stored on user computer).

Thank you :slight_smile:

I apricate the detailed response, please accept my apologies if I have explained it badly. Will try and clarify.

This has been done, the variable has been added and is being set by my User Interface Widget Blueprint

This is where the issue is, from your screen shots i can see your creating a child blueprint class from an actor, the quest system i implemented doesn’t use actors. The child is a of another blue print class that’s not an actor or a widget; as a result, when Cast to game instance i have nothing to use as a target thus causing a compile error.

Example of Code:

The error is:

This blueprint (self) is not a Widget, therefore ’ Target ’ must have a connection.

I have tried to plug in all sorts into the target but they all fail, without this i cannot obtain the variables so the output is always false so the quest never gets marked as complete.

Regards
Bobby

Try this, setup the GameInstance reference on Event Construct (the root of your widget) and define a variable for this GameInstance (like in my screenshot above). Use the same Get Game Instance, where target is not a widget. I suppose you are trying above code inside a widget and attempt to call the GameInstance from there.

Inside your function you use this GameInstance reference.

There is no event for Construct on the blueprint itself or its parent, i tried a few other events and nothing was listed for the normal ones.

Looking at the class in more detail; in the tutorial its a Abstract object blueprint class, which is like a root level blue print. And the actions such as quests themselves are added add as children of this class.

neither the parent or children are live elements, they are just functions if you like to trigger other things. I thought maybe i could cast a value from a widget UI to the tablet and then use the tablet to pass the information but that too seemed to fail with the same error, it just seems at some point the implemented method for this quest system is not the right one for complex quests.

I apricate you spending the time to respond however.

Basically you have the GameInstance a permanent place to handle public data during runtime and you can use widgets or other Blueprints like Actor or Character to communicate through all of them.

In order to send communication to the GameInstance, you first need a reference to it, setup a reference on start, this is for actors or the Character BP the Event BeginPlay.
For widgets you can use Event OnInitialized or Event OnConstruct. Then you can use this reference to access variables on the GameInstance.

To store runtime data you should keep it on the current Actor (i.e. a car blueprint) or on your Character BP or the Controller.

In your example you mean the target is the GameInstance? When working with child blueprint you only need to setup the references on the Parent blueprint, then you can use it throughout all the child bps.

Perhaps you are working inside an Interface. In that case there are only basic interface functions which then become accessible inside your actor bp - once you added the interface to that actor.

To add an interface to an actor navigate to the Class Settings panel inside the Actor view, and on the right sidebar find Interfaces…

To further assist you I need to know what kind of bp you are trying to edit.