How to cast an actor blueprint in widget?

Greetings. I have made an actor blueprint class and I want to use a variable in my widget UI. To do that, I know I have to cast to it and then get the variable. I am able to do it with character blueprints but not able to cast actor blueprints. What should I do? (Refer the attached images)

I want to get the ammo variable from weapon_bp to ui widget
Thank you

You need to use GetAllActorsOfClass to get a hold of the actual actor. You only need casting after that if you need to get a subclass.

Hello Sanchay Joshi,

I’d approach the problem by communicating the variable from the ‘Weapon_BP’ to the Widget. In OOP you want to encapsulate every behavior to their respective class, and expose as little information as possible.
The simplest way to do that would be to serve the ‘Weapon_BP’ blueprint the reference to the Widget (if you create the widget inside the player blueprint).
After that I’d create a custom event inside the widget blueprint, and give the value of the ‘Ammo’ value as an input and you can call it every time you call the ‘Fire’ event inside the ‘Weapon_BP’ blueprint.

The custom event inside the widget blueprint:

299514-custom-event-inside-widget-blueprint.png

Inside the ‘Weapon_BP’, inside the ‘Fire’ event, but after you update the ‘Ammo’ variable.

299515-inside-the-weapon-bp-blueprint.png

Just a friendly side note, the ‘Reload’ function would be better placed inside the ‘Fire’ event. Use the ‘Tick’ event as sparingly as possible and only for the necessary functionality.

Hope that helps. Just let me know if you need further assistance or if you have any questions.

Regards,

Vecherka.

Thanks. I figured a way out… For some reason, I am not able to implement your method (perhaps due to a technicality that I spawn the weapon on the hands of my character when game starts. What I did is, I casted the third person blueprint character, from it, get a reference to the spawned weapon available (a variable inside thirdperson) and then accessed variables. If you don’t mind, can you tell me something?:

  1. I use the tick function to automatically reload the weapon when ammo == 0. If I use this in shoot/fire function, what happens is that if I am firing endlessly (Just holding the LMB), value goes to negative values. How can I fix that?

Hi Sanchay Joshi,

you could check your ‘Ammo’ variable, via a branch node, before you actually shoot but inside the ‘Fire’ event. If it is less or equal zero you set a flag that you’re reloading and block the shooting functionality with that (i.e. an other if statement, before the ‘Ammo’ variable check). If the ‘Ammo’ variable is greater than zero the player should be able to shoot. The only thing you have to manage now is when and how you’re setting the ‘currently reloading’ flag back to false and setting your ‘Ammo’ variable. It depends on how you want to approach this problem and on your setup, you could, for example, do it via Animation Notifies (if you want to sync it to a reloading animation), or via a timer (timer/tick).
That said these are not the only and or best solutions, hope they help though. Just let me know, if you need screenshots or further help.

Regards,

Vecherka.