Why can't I cast to level blueprint?

I need to cast to the level blueprint to get info on what is the current level, so that I would remove the pause option in the menu

You can’t access the level blueprint from other classes, and you probably don’t want to anyway because each one will be different and specific to that level. While you can sort of do this in C++, I really wouldn’t recommend it anyway. You can however get the name of the current level, if you need to.

Level Blueprints are only really designed for map-specific logic (such as a one-off door opening or starting a level sequence etc) - you shouldn’t be putting pause menus or general game logic in there (I mean, you can if you really want to - but it’s far from the best approach). To be honest, I don’t think I’ve ever actually needed one.

A good design is to store and reference all of your UI from a HUD class, so that it’s managed from one central place that’s easily accessible. That way you can tell the pause menu to be removed from anywhere, even the level blueprint.

Hi man, thanks for your help.
My pause menu is a separate class, and I discovered that when the player is in the main menu I can call pause menu as well. Thanks for your detailed tutorial, I know understand level blueprins much better and I think I came up with idea how to fix my issue: I ll just make a variable in umg and set it to true when umg is created then I will check if it’s not true, then pause menu will appear, thanks a lot

This doesn’t quite relate to the original question but for the sake of people who run into this thread some time in the future I’d like to just retort to @ as there are genuine reasons why one would want to access the current LevelScript actor in code. A project of mine uses it to store all “adjacent” levels to a given streamed level and preload them while unloading the ones adjacent to the room the player came from. The game is structured as a series of rooms, think Metroid Prime style.

in addition to this every level script actor fetches all objects of type StreamingDoor contained within that level to populate the adjacent levels list so that the process is automated and designers only have to place doors around.

As a general rule of thumb though, if you have to access the level script actor you’ll know it and it is definitely something 99% of people can do without especially when just starting out.

Hmm I guess that’s fair enough - though in my case I would probably write a specific level streaming manager of some kind and access adjacent levels through that, each to their own though. I can only write from personal experience in this case, but I’ve never really used the Level Blueprint for much.

Level blueprints can be extremely useful if you have a lot of custom scripted events. Also, quest logic as the level blueprint is the closest thing to custom quest editor Level BP allows using a reference to any object in the level without a hassle.
But I would never recommend accessing level bp from outside in such case. Event/quest script should be the last thing in the script chain.

Use an event dispatcher on something persistent like GameMode or PlayerController. Bind an event to the dispatcher in level blueprint BeginPlay. Get a reference to the persistent object in whatever you want to call the dispatcher from, and then call it.

2022 here, with UE4.27.
So how can I access (Cast to > read) a variable in my Level blueprint???

I have a HUD that displays the Level number (and later I want to display the Long name (title) in a save slot). This is a level specific variable, thus I want only Level 1 to give me a value I can read as Level 1. Level 2, gives me 2…

So where can I set this level specific info, to Cast to in my HUD code > to then set/display as text on my HUD???
[I don’t want my HUD or Character BP to be a dumping ground of every variable I need in my game, when these are level specific variables.] Ty

Use the Game Instance. The GI is persistent between level changes.

Create a custom GI and set your game to use it in the options
On level load, have it cast to the new GI and store a variable
Your HUD can then cast to the new GI and read the value of the stored variable

Thanks for the reply. But I do not want persistence to other levels. I want a map specific variable, and 100% reliability that my “Level _” specific variables will get pulled/read by my [other actor code, e.g. Character and HUD BPs] - no matter what map I load - but be correctly matching the specific level parameters I designed. E.g. Level 1 has 1 key. Level 2 has 2.

  1. I found a tut for how to read GI to Level BP. But not reverse. (If a player loads my game, and Idk what level they select, then how can I know which specific GI to load? This is the problem of not having access to level specific BP - to tell everything else where the player is.)

  2. Then I went into the paradox of save game slots. But this isnt a solution. On begin play, I need a level specific actor telling the level which variables to use in only that level.

Yeah, you would be clearing or overwriting the variables on level load. You can store as many as you need, or use arrays or maps or w/e variable types you want. You can make them as organized or disorganized as you want. Other than that, you cannot cast to the level BP for a ton of reasons. Even if you code it into the engine in c++, it’s still not recommended because it can cause a lot of issues.

Basically, you’d just need to do some checks on level load. You can store all the progress for things like keys inside of the GI, which makes it nice for save files and stuff. So let’s say you load level 7 and there are three keys in it: On level load, cast to the GI and tell it “hey we’re on level 7 now” and then based off that information and depending on variable types, you’d then ask it “how many keys are here total and which ones do we have?” you’d see there’s three keys and you have keys A and C, but not B. All of that can be pulled from the GI, while in the level BP, by having it cast to the GI and then set local level BP variables or just have everything cast to it if it’s not going to destroy performance. Point is, all of that information can be stored in the GI and can be accessed from anywhere pretty much.

But who is telling the GI that we are on level 7??? This is why I need a level specific BP. So if I cant put the code in what Epic calls Level BP, then where do I put it?
I tried making a BP (that I intend to be level specific). It is a text component that says “Level 1.”

I tried to pull the name/string of “Level 1” - to cast to my Pause HUD as the displayed text. But it doesnt work.

Can you please be more specific with the nouns? Which IT are you referring to?

  • All of that [variables?] can be pulled from the GI, while in the level BP [do you mean I can use Castto GI, inside the Level BP? What is the code please],

  • by having it [you mean the Level BP?] cast to the GI and then set local level BP variables or just have everything cast to it [Level BP?]

  1. So what is the point of using Level BP for the above, if it’s so hard to communicate with Level BP?

  2. Should I just make my own BP called Level_Info_BP? Then place it in the level, have Editable variables (to manually change for each level)? (So far I do use a GI that says there’s X number of keys on this level. But I only made 1 level so far, thus I need a future level-specific method to tell the GI how many keys per level.)

  3. Then (On begin play) have my GI pull info from my Level_Info_BP, instead of bothering with Epic’s Level BP? I dont understand why they are restricting it if we can just make our own Level_Info_BP (yet we loose the shortcut button they made for theirs). TY

Each level will tell the GI what level you’re currently on. I’ll run you through this one more time:

Create a custom GI and set the game to use it
On level load like level 1 2 3 4 etc, within the level BP, you have it cast to the custom GI and update what level you’re on, along with any other variables you might want to set like key values and such
From there, anything that might need that information can now cast to the custom GI and pull the values of the variables.

  • Level begin play>Cast to custom GI>Set current level variable there and keys or w/e you want
  • HUD wants to know what level we are on>Cast to custom GI and read the values needed

I had valid use case for casting to a level:

The level plays music via spawn sound 2D on event begin play.

A trigger box stops the music, pauses the game, and opens a widget with audio & text (the background music is turned off so that you can hear the speech).

Couldn’t figure a way to get the music back on, as I cannot cast to the level from the widget and re-activate it (as it’s spawned in the level blueprint).

Yes, many ways around this, but casting to the level to activate the music again would’ve been the most intuitive one.