0

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 -> here are some details in a picture

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.

Community
  • 1
  • 1
Ilyas Ennaboulsi
  • 29
  • 1
  • 1
  • 7
  • I feel like your variable would be better suited in the game mode or something other than the level blueprint. It should be able to easily transfer to one of these since you only have functions and variables in it. Then you can call your game mode and access the variable from there, etc. All you need to do is Copy, Paste and recreate the variables. – Dtb49 Jun 05 '18 at 14:49

3 Answers3

2

Create an Actor Class for your logic/functionality. Then use Get all actors of class (choose your class) -> Get a copy -> get variable

enter image description here enter image description here

Community
  • 1
  • 1
Brian
  • 1,035
  • 7
  • 14
0

Communication with the level blueprint is rather tricky in UE4, since they are not as persistent as e.g. the GameMode, and therefore shouldn't be accessed directly (Imagine older games like Final Fantasy where a new level was loaded every time you stepped outside a boundary, so relying on it could potentially break your actors or crash the game due to nullptrs).


It's a little hacky, but works:

Move the variable inside the cube-blueprint. Add an event dispatcher to the cube, if it is moved, call it and pass the variable in.

Select the cube in the editor, open the level blueprint, right-click, "add reference to selected actor" (the cube must be part of a blueprintclass, not only a static mesh dragged in, though), and bind the event dispatcher inside the Level BP.

Create a function inside every blueprint that needs access to the variable, which does whatever it should do, depending on the variable.

The custom event of the Level Bp (that was bound to the Event Dispatcher of the cube), needs references to all actors that have to work, when the variable changes and call each Actors function (you can get the references like you got the one from the cube)

Then, every time the variable changes, the Level BP is notified, the custom Event is executed and this custom event calls all the actor's functions.

Event Dispatchers explained


This is a huge wastage of functions/code, since you only need it for this one level and may never use it again. Avoid this in the future, by not relying on the level BP so much.

0

You can use GameStateBP to create and store all variables that you need in game, in GameModeBP create functions to get and set this variables via Get Game State function and then function Cast To GameState and then logic. After that from any blueprint access it using Get Game Mode -> Cast to you GameMode -> use your function to set or get data from GameState.

Mike
  • 185
  • 1
  • 2
  • 13