I have a npc blueprint BP_NPC with a Widget Component. In this Widget I have the health bar and i want to set percent with the event tick of the Widget Blueprint. I want to pass the BP_NPC reference to the Widget Component but i don’t know how i can do it. If i set the widget of the component to the widget blueprint itselfon beginplay of the BP_NPC, i get two copies of the same widget, one on top of the other, and if i set visibility to false, i cannot see any of the two Widgets. Does someone know how i can get it? I would really like to continue having the widget blueprint instead of a variable, because i can set it to World Space and appears on top of the NPC. Thanks in advance!
To access the widget:
In this Widget I have the health bar and i want to set percent with the event tick of the Widget Blueprint.
It’s not a great approach. Consider telling the widget what to do, rather than having it pull data out of other objects. UI should display what you tell it to, not search for data.
If you must do it this way (hey, it’s your project!), create a variable of BP_NPC type inside the widget and SET it on the BP_NPC’s Begin Play using the nodes above. Also, since you’re going to use Tick anyway, you might as well use property binding - it’s polled every frame:
- in the widget, create a reference variable:
The above assumes Health
is in 0-1 range already.
- in the NPC:
It’s not that bad tbh, and should work well in some scenarios despite being very tightly coupled.
Thanks for your fast reply! I used your solution but i still have two or three copies rendered in game!
This is the BP_NPC Event Graph:
This is what it looks before pressing play:
And this is in-game:
In fact, there are 3 copies of the UI.
I tried Deactivate the component and Activate after the NPC instance is set, but i still get the same result.
Hard to tell without seeing the script / rest of the setup.
- the widget component instantiates a widget already - perhaps you add extra widgets elsewhere? Definitely do not do this:
- perhaps there’s more than 1 component per actor
In fact, there are 3 copies of the UI.
Do you mean in total? Or Each of the little armed dudes has 3 widgets? Not sure what I’m looking at:
And not sure how it’s supposed to look like. Could you post the NPC component hierarchy and its widget hierarchy?
Ok sorry, i do have a few more components that are rendering at the same time. i thought all the texts and the percent bar were in the same Widget Component,but it’s been a while i don’t touch this part of the project and i forgot it, my bad! I had 3 text renders with the name, employment and level of the npc, and they were replicating because i had the binding already on the Widget Component, a pretty big mess . So now I only have the Widget component and everything in the component hyerarchy is fine now.
However it seems that the Health Bar is not being refreshed properly once i apply damage. I hit attack one time (each attack has 10 base damage) and the percent bar goes from 1 to 0 instead of 0.9. This is the binding i created:
and this the gameplay in which I attack one time and the bar goes to 0 and at the end i do 10 attacks and kill the enemy, so the damage calculation is good but the visuals don’t match. When the sound is played, it means that i’m attacking (it’s printed aswell as “I Attack” prompt)
I have current and max health because it’s a RPG project and you and the enemies can level up and get more max health. Both are Integers. Do i need another variable for each one (current and max health) on a 0-1 range representation?
You need to divide floats instead of integers.
Oopsie,working now fine! Thanks a lot, really appreciate all the support!
This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.