Reading/Writing Variables

I’ve looked at just about everything I could find on this (Wiki, AnswersHub, tutorials, forums posts) and still can’t seem to figure out how to do this or what I’m doing wrong. I feel like I’m missing something obvious.

I want one blueprint to be able to read a variable from another blueprint, it doesn’t even need to be able to change it (although that will be useful eventually I’m sure).

The only way I could find that it even comes close to working is to use a function to pass the variable but that only works because in one specific case that blueprint is the player character so I can use the “Get Player Character” as the target when I’m executing the function in a graph.

I’ve tried creating variables linked to blueprints like it says on the wiki but I just get the “access none” error and no matter what it just doesn’t work (even when I try setting the specific instance, although this isn’t always possible when using a non-placeable blueprint like a hud)

Anyone have any ideas? It seems like this should be really easy. Ideally I would have some global blueprints that would contain global variables that could be read or written to but so far that doesn’t seem possible?

Thanks!

You may be able to do something like this with Event Dispatchers… I haven’t particularly played around with them, only saw some usage in youtube tutorials for UE4. Maybe it can point you in the right direction until someone else more knowledgeable than I comes along. :slight_smile:

I am also interested in this, event dispatchers seems the way to go but I am yet to get my head around how they work.

Have you tried using a Blueprint Interface? I’m an animator and have used interfaces to pass data between character blueprints and animation blueprints.

More info: Blueprint Interface | Unreal Engine Documentation

I’ve tried creating some dispatchers, and have read through the documentation and watched some of the video tutorial on them, but from what I can tell you need to be able to access the instance of the prefab in the level because to create/bind events related to dispatchers is context sensitive. So in that case they don’t appear to work for something like HUD blueprints.

Again though, that may be where I’m just not understanding how they’re used :frowning:

I was able to use an interface function when grabbing a variable from a player character but if I want to grab from another blueprint I run into problems targeting it.

Thanks for the replies. There’s a couple things I’ve worked out on this:

From what I can tell what I was trying originally, direct blueprint communication, has a number of restrictions:

1.) Both blueprints you want talking to each other need to be placed in the scene. The ONLY exception to this is the level blueprint. If you create a variable in a level blueprint that targets a blueprint in the scene then opening the level blueprints details in the property matrix allows you to select the instance.

This does NOT work with any other non-placed blueprint (game mode, hud, etc). You can open the details in the property matrix, can select the instance (it still has to be placed) but it doesn’t save it. Not sure if this is a bug.

So, for example, as far as I can tell you can’t have a global variable blueprint and have your game mode and hud directly talk to it.

2.) You can use the game mode, player controller and player character blueprints as a means of passing/storing variables because they can be directly referenced using the green functions “Get Player Character”, “Get Player Controller”, “Get Game Mode”

As Ray suggested, you can use a blueprint interface to create simple functions that send/receive variables to/from these blueprints. I have a feeling this could have some drawbacks, but so far looks like it will work.

For example, you could have a script that fires a function to update a variable in the game mode, then have the hud pull that variable from the game mode.

But you couldn’t have a function in the game mode or hud pull a variable from the global script, because there’s no way to target it. Again, not sure if that’s intended or a bug.

Could you give a little more detail on what you are trying to do? Accessing variables from one Blueprint to another should be easy, but you do need a reference to an instance to do it. There are lots of ways you can get this though - lets assume you have two BPs called Barrel and BarrelManager, and BarrelManager wants to change a variable on a Barrel.

  • Add a ‘Barrel’ type variable called TheBarrel to BarrelManager and make it ‘editable’ (click the little eyeball next to the var). Now place a BarrelManager and a Barrel in the map, select the BarrelManager and use the ‘Actor eyedropper’ to ‘fill in’ TheBarrel to point at the Barrel you placed. You could also change TheBarrel to be an array of Barrels and then iterate over them use a For Each loop.

  • Inside BarrelManager use the ‘Get All Actors Of Class’ node to find all Barrels in your level and modify them

  • Have the BarrelManager do a raycast or overlap check to find nearby Actors, then use a cast node to see if it is a Barrel and then modify it if so.

I hope that helps a little! I’m going to make another Quick Shot video tutorial on this topic soon I think.

As an example of what I was trying to do:

Say you had gold in your game, you want blueprint instances in your level to give/take gold, you want that to update the current gold you have and you want that represented on the HUD.

Right now the only way I could get this behavior to work was to use functions in a blueprint interface, execute one of those function in the blueprint instance, which updates a value in the game mode. Then have a function executed in the HUD that grabs the value from the game mode.

The only reason why that works is because of the “Get Player Character”, “Get Player Controller”, “Get Game Mode” nodes which output objects, and those can be hooked into the blueprint interface functions that are being executed in the instance and HUD.

That’s the only way I could see to update the HUD, for example, because it can’t talk to any other blueprint except those three exceptions.

Maybe that’s the intended workflow, there’s just no mention of that anywhere that I could find so it took awhile to find those and get my head around it.

So I’ve been through the same problem, I was able to actualise my hud with some player variable that I was not intend to use.

I also notice that if I drag and drop the variable from the Level BP to the Hud BP it apear, but do not work, I belive that’s becouse lacks reference.

If you’re using a custom HUD and need to reference your particular instance of that HUD being used in-game, you can use the Cast To function to be able to access the variables (and functions) on your HUD for read and write. Here’s what that setup could look like:


If you have a setup that is more complicated and needs further explanation, please post screenshots of your graphs and we can see where you might be hitting blockers or can make changes.

Hope this helps! :slight_smile: