Access a variable from level blueprint

Hello , I Have a variable that updates every time i move my cube in the level blueprint , now i want to access this variable from multiple class blueprints , what do I do , I tried casting to gamestate but didn’t succeed , I am really new to ue4 if you could explain in details please.

edit: sorry for not adding details , The var I want to access is an integer named cube_side that tells me what side the cube is on every time I move , all of this happens in the level bp , I want to access this variable to see what side the cube is on from other class blueprints.

I know it’s not good to code everything in the level blueprint , but it’s too late now , I only need to transfer the var cube_side to other class blueprints so the other object can change depending what side the cube is on.

I totally agree with @Xosh, however, if you really want to do what you want here is how.

  1. Create your Game State: Your thought on the game state is correct. Is should track, well, the state of your game. So, create a blueprint class that inherits from GameStateBase. Let’s call it BP_GameState.
  2. Open BP_GameState blueprint and create a variable that will be set by the LevelBlueprint. Let’s call it My Var From Level Blueprint.
  3. In your LevelBlueprint, where you can calculate some state of your game, you will get the current game state, cast it to your game state and set the variable My Var From LevelBlueprint.
  4. In your Actor Blueprint, you will get the game state, cast it to your game and then you can read your variable.

Hope this makes sense. Following there are some images showing the process I just described.

In the LevelBlueprint

In your Actor Blueprint

It’s the good occasion to transfer your code to an actor. When it will be an actor you can easily get access by «casting».

Have a great day!

Hello thank you for your answer I have Tried it but it doesn’t work the cast in the class blueprint fails , here are some screens so you could see , I did everything you said but it didn’t work maybe I messed it up some were , if you could correct me please.

Pretty sure you can’t “cast to Level BP”…or pull variables from the “Level BP” outside of the level, hence why using it is such a bad idea 90% of the time. It does have its uses, but not for things that you want to access in other classes.

You’re right, however, the trick is to pull from the GameState not directly from the LevelBlueprint, and that is perfectly reasonable. You have some state computed by the level and you want actors to have access to that state. I think I forgot to mention, but you need to set your BP_GameState as the default game state. In order to to this, go to EditProject SettingsMaps and Modes and make sure that the Game State Class is your BP_GameState. The cast won’t fail when you do this.

I tried your method myself since I was unaware of it and wanted to see if it worked. I had set up my game mode and game state and put a variable in the level BP then went to a player BP to try and access it through game state or game mode neither worked. So not sure how you’re doing it but I couldn’t get it to work either

I have made a minimal example showing what I am describing. You can find the example here.
In this example, the Level Blueprint will, every second, increment a local variable, starting from 0. After the increment, the Level Blueprint will set this variable in the BP_GameState. The Game State will do nothing with it, it will only store it. I placed in the world a blueprint called BP_SomeActor. This actor has a TextRender component. On the Tick event, this actor will read the variable from the BP_GameState and will set this value as the text in the TextRender. This is how you can “read” a value from the level blueprint. You cannot read it directly, but you can one the Game State to store any value you want actors to be able to access.

I guess what I am trying to say is, there is no way that I know of to access a variable that is ONLY present in the Level BP by any other BP other than the level itself. Can you create a work around to get info from the Level BP into another actor, sure and your method would work as would any “actor” not just game_state. What you are describing is a work around for the OP which is fine, that is what he wanted BUT for clarity this isn’t a method for accessing a variable in the level BP from another actor. What you are doing is transferring the data from the Level BP to an actor that can be accessed by other actors. And you mention that you can’t pull “directly” from the level BP so we agree. So I guess my point is, that will work, BUT so will creating a variable in “game instance”, “player controller”, “player character”, “random actor” etc and having the Level BP get a reference to that actor and “set” the variable within that actor to the same value as the Level is holding. Regardless you end up having extra code because you have the same data in the Level AND another actor. Better to just start with it in “another” actor was my original point haha

You are totally right. There is no way to “pull” something directly from the Level Blueprint, at least none that I know of =).

Thank you I did what you said “go to Edit → Project Settings → Maps and Modes and make sure that the Game State Class is your BP_GameState. The cast won’t fail when you do this.” and it worked :slight_smile: