Search code examples
unreal-engine5blueprint

How to get a variable from another blueprint in UE5?


I am following a tutorial which can be found here:

https://www.youtube.com/watch?v=3P1A73Ghisw

Anyway I have 2 blueprints, 1 that is going to be a checkpoint for my racetrack and I created a boolean within this called Is Finish Line and I need to reference that in my other blueprint: GM_RacingGame where all my gamemode settings are. This is what I have:

Picture of Variable

Picture of gamemode script(Notice context sensitive is on)

Context sensitive off showing that I can place it into the blueprint

Showing how we cannot connect anything because of type mismatch

So far I notice I can only see the choice when context sensitive is off. Why is this and how do I fix it?


Solution

  • The issue lies in the type that the ForEach-loop reports on its return-value. It says that the "Array Element" is of type "Actor Object", which is a base-class of your BP Checkpoint Object. Obviously, this does not have the "Is Finish Line" variable, so that's why the context sensitive menu (and the attempt at connection fails).

    In general, Unreal is strongly typed, so if you have a "Actor Object", and want to get a "BP Checkpoint Object", you would have to cast down (with the cast node). However, please hold on to it for now, as there is one other possible explanation in your example.

    See, GetAllActorsOfClass should actually return an array of the correct type. Meaning if you connect a ForEachLoop, the "Array Element" should be BP Checkpoint. I think what might have happened is that you created the ForEach-loop before, and had another array connected (or possibly the GetAllActorsOfClass-node without a selected class). This would cause the ForEach-loop to select the wrong type for its return-value, and unfortunately wildcard-nodes in Unreal (which ForEach is), won't refresh their types to match up automatically. Try one of two things

    1. Force-refreshing the ForEach-Loop node, via Right-click context menu on it
    2. If that doesn't help, create a new ForEach-Loop, and connect it directly to your GetAllActorsOfClass

    Then it should work as intended.