Using a variable on two blueprints

First of all, there’s a lot of questions about this on the web but none of the answers are working for me. They’re confusing or just not working.

I am VERY new to blueprint scripting and have a few hours of experience, so please go easy.

Basically, I have a UI for my ammo that I want to access from the blueprint that fires my gun. This sounds simple enough but I’m tearing my hair out because I can’t seem to get my UI from the gun firing blueprint. I just want to take this variable
102952-
and use it here:

Hi Klink45,

What you will need to do is get a reference to the actor containing the variable and do casting, which can be found here:

https://docs.unrealengine.com/latest/INT/Engine/Blueprints/UserGuide/CastNodes/

If you have the time, I encourage you to watch the presentation we have on Blueprint Communication, it is extremely helpful in learning how to properly set up communications.

https://docs.unrealengine.com/latest/INT/Videos/PLZlv_N0_O1gYuWkyTSbfcuyPNOQsK_8bu/EM_HYqQdToE/index.html

Hello, so there are several ways you can do this based on your setup. Generally I use one of my blueprints as my “MasterControl” blueprint, basically the one I use to link all my references. This is generally the blueprint that spawns my UI for ease of use. So when I createmy UI elements I make sure they have a variable in them named “MasterControl”. This way, when I spawn my UI, I can set the “MasterControl” variable and have a link to everything else I may need.

So in your case, the player pawn would be a good “MasterControl”. In your player character I would keep a reference to your “UI” and your “Gun”. In your “Gun” you can have an “AmmoCount” variable. In your “UI” you would have a “MasterControl” variable. Then in your “UI” you would get a reference to your “MasterControl” variable, then drag out from that “Gun” then drag out from that “AmmoCount”.

Make sure each of these variables are the same type of actor as the item, or you will have to cast to the correct type each time.

I hope this makes sense. Let me know if you need more clarification.

Thank you so much for replying! I am sorry but I’m confused with that answer. Mainly, how can I connect the ammo variable in my gun to my UI? A picture would be nice.

Ok so image is in the UI Widget. I made a text field and created a binding under the textbox content. There is a dropdown that says “Bind”. Click it and select “Create Binding”

I am using 3 blueprints
“LinkExampleGun_BP” which contains 2 variables:
MasterControl - type “LinkExampleMC_BP”
Ammo - type “Integer”
MaxAmmo - type “Integer”

“LinkExampleMC_BP” which contains 2 variables:
Gun - type “Gun_BP”
UI - type “LinkExampleUI_WIDGE”

“LinkExampleUI_WIDGE” which contains 1 variable:
MasterControl - type “LinkExampleMC_BP”

You just need to make sure that you properly fill in the references. I do this by spawning the Gun and UI in the “MasterControl_BP”, setting their value, and immediately setting the MasterControl variable in them.

You can set these a variety of ways, but this is the way I would do it without knowing anything else about your setup.

Let me know if this works or if you have any other questions.

Thank you. I am still having troubles but at this point it is quite unrelated: Get the target? - UI - Epic Developer Community Forums

Actually, if you followed the above method it should solve that issue as it sets up the references to all the blueprints in the chain. You should just need to plug the reference to the correct blueprint into the “target” pin. If you need to go from the “Gun” to the “UI” or the other way around, you will need to grab that reference from the “Master Control” reference as I did in the 1st image to go from the “UI” to the “Gun”. These same methods should allow you to not only get variables but call events and functions from another blueprint.