Setting another scripts variable

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By LionHeart40

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!

:bust_in_silhouette: Reply From: Thomas Karcher

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: