Object reference variable

Hello everyone! I’m started learning blueprints, and can’t figure out this thing. Please explain it to me)
I’ve got two bp - test sphere, test cube. In bp_test_cube I’m calling add Impulse, for test sphere as the target. If I’m using a cast to bp sphere, for getting sphere reference everything works fine. If I’m making variable referencing to bp_test_sphere and try to use it as a reference it doesn’t work. The question is - what purpose of actor reference variables? Why when I make variable referencing to the actor, it doesn’t work as a target for add impulse? What is the best way to reference one blueprint in another one?

This is working bp with cast

This one don’t work

227504-variable.png

Hey there welcome to the unreal 4 community!

The reason you create Object reference is so that you don’t have to cast every time you wanna reference an object.

In the situation you showed above (the one that works) on the event hit you are checking if the hit actor is a “BP_test_sphere” blueprint object. So When you hit the correct object it will let you access all the values inside that blueprint. In programming terms this is how unreal 4 handles Object oriented programming with blueprint; in other words how blueprints communicate with each other.

As for whats the best way to get an object reference?

The answer is almost right in front of you! For example use the “event begin play” node pull of that, cast to the object you want, use “get owner” as the object input. after that right click the object pin and click “promote to a variable”. And voila! You now have a reference to that object.

I recommend reading through the unreal 4 documentation and/or watching some youtube videos on the subject.
Casting Quick Start Guide | Unreal Engine Documentation


Here are some links and a screenshot of what i described above, I Hope this answer was somewhat helpful!

Happy coding!

  • SamuelB

Edit: In the second Picture you are not checking if the Hit actor is equal to the referenced object. So you have to drag off the “hit actor” pin and check if its equal to the object. Then you branch of the boolean value from that and execute add impulse if its true. =)