How to get access to a variable located in the Level Blueprint?

The editor lets us add variables into the Level Blueprint, but I didn’t find any way to access them from blueprints located into that level.

I know a similar topic has been brought up here but it diverged into “use another method” or event dispatcher but the latter is only one-way communication as I understand it (only signaling).

Here as I am really asking how to get access to a variable located in the Level Blueprint and not some other workaround. Using the GameMode or GameState instead is not an option when the game is not running, i.e. when using BP with Construction Script running in Editor, GameMode/GameState are non existing.

Hi Rexuyo!

I understand what you’re saying, but we need to get some more information to get together a plan for what you’re wanting to do. You can’t cast to Level BP, the code just doesn’t work. Could you possibly get together some screenshots and information on these variables you’re trying to get from the level BP so we can understand what functionality you’re trying to create? You may not be able to cast to LevelBP but we can likely still do something relatively simple to achieve what you want.

GameState and GameMode won’t work in Construction Script, you are correct there. A Construction Script needing to pull a variable from a level BP is a rare case, but I have many ideas to achieve what you’re needing based on why you need it. For instance, an enemy may have a different static mesh based on the Level. To do this The construction script would check for LevelName and set static mesh based on that name, and then you could set level name in Construction Script to check functionality.

Get us some more info, a description of your intentions for getting variables from the Level BP, some screenshots of your code, and let’s get this!

Thanks for the quick reply! Ok I’m going to detail below why I’m looking for this, but even though I am giving here a specific example, the more global concern is “how to store level scope global data from construction script BP while the game is not running, and still have the concept working while the game is running”.

I am following this great tutorial on RVT landscape. The concern is the creation of a dynamic material instance in the construction script of the spline BP, hence each road spline will create its own dynamic material instance. I would like to create the dynamic material instance once, store it globally so other BPs can reuse it. Problem is:

  • there is no GameMode/GameState when the game is not running.
  • Level BP variables cannot be accessed (!)

I could create a specific BP to store the global data in that level, it would be unique with a specific class, I could get it by class name. That would work in editor, but not at runtime, since I have no guaranty this BP will be instantiated before the spline BP. So what should work I guess is a hybrid system, where while not in-game I store into a specific BP, and while in-game I store into the GameMode. All this because we cannot access variables in the Level BP :slight_smile:

Update: I realize the example with dynamic material instance is not a good one, as it this case it makes sense to create one per BP instance, since we want to control the material properties from the BP and make the modification specific to this BP. However in a more general way, it would be nice to know if there is a way to share data between blueprint in the editor level-wide.

Hey again, Rexuyo!

I think there may be a misunderstanding, but nothing we can’t figure out! So, let me throw out some general knowledge I have and see if you can figure out your issue from that, or at least we might be able to see where the communication might be getting lost.

Once something like a dynamic material instance is created, it is stored in the program files. This doesn’t have anything to do with the level BP. It can be retrieved by anything in the game.

Say you would like each instance of this object to be customizable as far as the material goes. First you’ll promote that to a variable, like so:

Next, you’ll want to click this little eyeball here in the variables section of your actor and open that eye.
image

And what that will do is allow you to change this in the level editor!
I have the default to the base material, but as you can see here, you can change it per instance of the actor this way, instead of creating whole new actors, so for your road you’ll only need one road BP that you can reuse again and again forever.



Now if you want to set materials for something based on what level the player is currently in, you’ll have to call that level name and do a switch based on level name to set the material.

I hope this helped!

Now if you’re wanting to just have information stored IN the level it’s going to have to be stored in an actor. You could slightly delay an actor by putting the construction script you have on Event Begin Play and add a small delay node before the rest of the code, but it’s not recommended. It would be feasible, it’s just not ideal.

Get back to us with your progress, we’d really like to help you figure out the best way to go about your project!

Thanks, there is still something I’d like to clarify on creating Dynamic Material Instance in the Construction Script.

I don’t quite understand what you mean by “(a dynamic material instance) is stored in the program files”. Let’ say we create a dynamic material instance in the construction script (CS) of a spline BP. The interest doing this is we can control the material from within the BP parameters. But when we’re calling “create dynamic material instance” in the CS, we never release it, don’t we have a memory leak there? Moreover, every time we change the spline shape, the construction script is called again, so we this would leak even more (in editor that is, not in game).

image