Getting a Variable from another Blueprint

So I have a simple test I wanted to do in order to understand casting and the sharing of variables from blueprint to blueprint. I am using the third person template and I created a blueprint called “BoxTest”. Essentially, it is a collision box in my scene that when the character overlaps it the BoxTest blueprint sets a boolean variable called “InBoxCheck”(which is set to public, IE the little eye is open) to true if the character is in it and then sets it to false once he walks out. It also spits out some text to let me know what state the boolean is in.

This works fine. The problem I have is in the next bit of code I want to do. I made a key press event in “Third Person Character” BP that when I press “Q” It casts to “BoxTest” BP, gets the state of “InBoxCheck”, and depending on if the boolean is true or false it spits out some text.

My question is what do I put in that object wildcard under the cast in order for it to work. Am I understanding casting right that it allows you to get the information from other blueprints to use in another blueprint. If not please let me know how this works or point me in the direction of some good videos or articles that explain it. Thank you!

You would connect your Trigger Box to the object pin, but you can’t directly do that in the Character BP as you would do that in Level BP. So you have to save that Trigger Box to a variable first. How exactly you do that depends on what you want to achieve. E.g., you can create an empty Trigger variable in the Character BP and set it to your Trigger Box once you overlap it (and maybe back to Null on End overlap, but if the variable is Null, Casting will always fail); or you can keep the variable with the Trigger Box value, and set it to something else when your Character overlaps something else.

Anyway, to be able to Cast to Box Test you have to get it in the Character BP first. You can use Get All Actors Of Class, of Get Actors With Tag to find it.

Am I understanding casting right that
it allows you to get the information
from other blueprints to use in
another blueprint

That’s true enough, but that isn’t exactly what casting is.

Casting is more like converting, or re-interpreting, an object reference as a more specific object type. For example, let’s say you have a APawn reference that is passed through a function as its more general type, AActor. In this state, you could only do AActor-related things with this reference. If you needed to do Pawn-specific actions on it instead, you’d first have to cast it from its more general type (AActor) to the specific type, APawn. But remember, you can do this only because APawn derives from AActor. By contrast, you could not cast an APawn reference into, say, a UWidgetComponent, because neither of those types derive from each other.

So, in other words, casting is like saying, “I have an object reference that I know can be converted into a more specific type, so please give me back a reference in the form of the more specific type so I can do specific stuff with it.”

In your case, assuming your “BoxTest” blueprint is deriving from the ATriggerBox class, you’d be able to cast a ATriggerBox reference into a BoxTest reference and then access the custom variables and functions you’ve created in your BoxTest blueprint.

Hope that helps clear things up a bit.

Thank you all for the replies! It put me on the right track and after a few hours of tutorials and articles these are two “fixes” that I cam up with. I will explain them the best I can so others might be able to learn.

The first fix is using “Get all Actors of Class” inside the Third Person Character blueprint.

So I guess in this instance I don’t even need to cast to my “BoxTest” BP. It did work but the cast gave me a Note at the bottom saying that I didn’t need to do it as I already had a reference to it. I simply used “Get all Actors of Class” and from the out actors I used “get(a ref)” under the utilities for Array. Since I only had 1 BoxTest in my scene I knew that the index would be 0. If I were working with multiple “BoxTest” blueprints in my scene I would have to find a way to specify which instance of it I wanted by knowing what index it is at in the Array. But since there is only 1 this works fine. From there I got the boolean “InBoxCheck” but I had to uncheck the “Context Sensitive” box as It wasn’t coming up. After that it worked fine.

The second fix I did was doing the keyboard event in the level BP.

This one was super simple. All I did was click on the box in my scene then go into the level blueprint and right click on the event graph. Right at the top there was a selection called “Create a Reference to Box”. I clicked it and did the same steps as before just without the “Get all Actors of Class”.

Thank you all for the help. Let me know if this looks right or I can do something simpler. I hope me explaining what I did helps someone else out because it seems so hard to share variables in UE4.

1 Like

Thank you very much this helped me alot!

Thank you for that!

So let me get this straight: you need to know beforehand what generates the hit? And what if actors of different classes can hit the same actor? How would you know what to cast?