But now I want to be able to access it from an other BP class. For example an actor which when is clicked the widget will be destroyed.
I tried making a variable in the level blueprint that will keep reference to the widget but I can’t access that variable from another BP. I found the “Direct BP communication” that should do the job but it looks like it does not work that way with the level BP.
What is the right way to do this? How to get reference to this widget from another BP?
Putting code in your level blueprint is really not a good ideal. Anyway, there is two way to reference to your widget:
Create a custom player controller blueprint and a custom game mode, then set those as your project default. Move the code to add your widget to viewport from your level blueprint to the custom player controller, then assign your newly created widget to a variable. Set that variable to public. Each time you want to reference that widget, just get player controller, cast to your custom player controller, then get that widget variable.
If you are using version 4.9, i think there is a blueprint node get all actor of class, where you can get all the instance of that widget class, get the first instance and just do your thing with that instance, but this only work if you are sure that there is only one widget at all time, also, this is bad practice in my opinion and may make your code easier to make errors.
Well, one of the bad point of putting variable and function into level blueprint is that i can’t for the love of god access those variable and function, been asking how to do that since 4.6. Another reason is that those variable and function will only existed and can be referenced in that level, so if you create a new level, you can’t use those functions/ variables anymore. Been working with this engine for around 1 year, i have learn that whatever i can put in level blueprint, custom player controller/ hud controller can be a better candidate to hold those.
Those bad point also level blueprint good point, if you want to hide some functions/ variables that can only be used in that level, such as dynamically setting lighting/ post effect/,… that you haven’t think of a good place to put, level blueprint maybe a good choice. Testing a blueprint function is also a good choice, because level blueprint can directly reference to an actor on the map without all those annoyed get all actor of class/ cast to class,…Other than that, try to use your level blueprint as little as possible.
Yeah I came up with some workarounds about that problem. I was just wondering if it’s possible to access variables from the LevelBP. Can you please tell me why it’s bad putting code in the level BP and when I should use it?