Search in
Sort by:

Question Status:

Search help

  • Simple searches use one or more words. Separate the words with spaces (cat dog) to search cat,dog or both. Separate the words with plus signs (cat +dog) to search for items that may contain cat but must contain dog.
  • You can further refine your search on the search results page, where you can search by keywords, author, topic. These can be combined with each other. Examples
    • cat dog --matches anything with cat,dog or both
    • cat +dog --searches for cat +dog where dog is a mandatory term
    • cat -dog -- searches for cat excluding any result containing dog
    • [cats] —will restrict your search to results with topic named "cats"
    • [cats] [dogs] —will restrict your search to results with both topics, "cats", and "dogs"

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. alt text

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. alt 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!

Product Version: UE 4.21
boxeg.png (152.8 kB)
thirdpersoneg.png (128.2 kB)
more ▼

asked Dec 12 '18 at 05:00 AM in Blueprint Scripting

avatar image

1 1 2

avatar image Tuerer Dec 12 '18 at 06:51 AM

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.

avatar image the_batch Dec 12 '18 at 05:51 PM

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.

(comments are locked)
10|2000 characters needed characters left
Viewable by all users

1 answer: sort voted first

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. alt text

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. alt text

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.

more ▼

answered Dec 12 '18 at 07:23 PM

avatar image

1 1 2

avatar image Jozeb Jun 12 '19 at 06:00 AM

Thank you very much this helped me alot!

(comments are locked)
10|2000 characters needed characters left
Viewable by all users
Your answer
toggle preview:

Up to 5 attachments (including images) can be used with a maximum of 5.2 MB each and 5.2 MB total.

Follow this question

Once you sign in you will be able to subscribe for any updates here

Answers to this question