0 votes

Hi there,

So I am using a dictionary to store all my stats into one file to keep track of everything easily.
Now I am trying to create a block where when you enter it, it deals damage to the player, updating the dictionary script. So far this works perfectly, however, for some reason I cant get my healthbar UI to update. I have a script attached to the "damage block" as I when I deal damage, I want it to come from the area rather than having to code it in the normal player script or in the UI script.

Currently I am using the get_node() function and it seems to be working correctly as it prints out that new value, but the UI is not updating with this new value. If I code the damage event within the UI script it works fine, but as I said above, I dont want to end up with having to code every single damage hit from every object or enemy into the UI script.

Is there anyway to update the UI script from the "damage block" script? Or am I stuck having to code it into the UI script?

Any help will be much appriciated!

Damage block script

UI Script

Godot version 3.2.3
in Engine by (15 points)

1 Answer

+1 vote
Best answer

Using a dictionary like this doesn't do what you think it does: By assigning a value from this dictionary to a local health variable, you're not creating a reference and changing the local health variable won't update the dictionary. That's why whatever you update in Hazard.gd will immediately be overwritten by the old value in the _process function of PlayerUI.gd

Taking out the first line in _process would probably fix this issue, but there will be more. For a better solution, have a look at Singletons in general and the Godot implementation described here:

https://docs.godotengine.org/en/stable/getting_started/step_by_step/singletons_autoload.html

by (1,696 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.