How to update score in a GUI

Godot Version

Godot 4.2

Question

I’m working on my first little game and I’ve run into a problem: the score doesn’t update at all when collecting coins. I followed several video tutorials, but none of them helped me to actually get the GUI working completely.

It does appear in the game and everything works when I test it, it’s just that the score doesn’t update.

I’ll include the scripts I’ve made so far for the game, the player, the coin and the GUI for reference.





I suppose you wanted to call currentGame.update_score(body.value), not currentGame.update_score_number(body.value). The function update_score_number doesn’t accept any arguments and doesn’t even change the internal variable score.

1 Like

Thanks for the tip, but it didn’t fix my problem sadly; the score still stays at zero when I collect a coin.

I’m very interested in how this works for you, but how does the GUI know that the coin has a value of 10? I’m not entirely familiar with the reference.

I think you are accidently skipping to the last step in a chain you set up… but you need to put your data in at the beginning of the chain for it to work, if that makes sense?
It looks like the game node wants to take in your data in the update_gui_score(value) function, pass it to the gui node function update_score(value), which then triggers update_score_number()

Your code tries to jump right to the last step.

First, double check the export variable currentGame is set to your game node.

Then in the _on_area_2d_body_entered function, call

currentGame.update_gui_score(body.value)

Did both of these, but the score still isn’t updating when I run the game.

What do you mean by putting data at the beginning of the game? Would I need to move the _on_area_2d_body_entered function somewhere else?

The coin needs an instruction to pass its value. The coin is deleted in queue_free() and the value is also lost. I think I would pass the value from the coin and not try to do it via the player. Unless you are passing the value to players.

ahhh, I bet thats right, _on_body_entered in coin.gd is probably firing before _on_area_2d_body_entered in player.gd
SO, by the time _on_area_2d_body_entered asks for the body.value,
_on_body_entered has already deleted it.

A huge part of code is making sure the information is flowing from one place to the next, and ensuring everything is called in the right order.

you are calling _on_body_entered from _on_area_2d_body_entered so I think the signal connection on _on_body_entered is just messing up the order in which things are happening.

I think this would all be a bit easier to follow if your function and variable names were a little more descriptive… there is no such thing as TOO descriptive. For example, in coin.gd if value were in stead called coin_value it’ll make the script a little less extensible but much more readable.

its seems like all _on_body_entered is doing is deleting itself. Maybe you need a function called delete_self and then it’ll be much clearer if you are calling it in more than one place.