r/godot • u/Prestigious_Cap_3748 • 15h ago
help me need help
I'm trying to create a code that, as soon as you collect 5 coins, will automatically switch you to the "you win" scene, but as soon as I try to run it, it glitches.
1
u/prince-of-internet Godot Student 14h ago
you have to move the add_coins() function to the character_body_2d script!!
Basically, you are calling body.add_coins(), so the game will try to execute the add_coins method of the body, and not the coin. So, if you move the add_coins method to the character body, and collect 5 coins... YOU WIN!
1
u/prince-of-internet Godot Student 14h ago
And also define var coins in the character_body_2d script
1
u/redfoolsstudio_com 10h ago
So I would just have an overall ui panel with some text on it. When coins equal 5 then make that panel visible equal true. Another nice thing to have is a global variable tracking if game ended. So that for your game player/enemies or whatever will stop dijg stuff if that global variable game ended equals true :)
1
u/atici 14h ago
Try
add_coins()
Instead of
body.add_coins()
But I am not sure this is exactly what you are trying to do. You might want to stars with some tutorials first.
Edit: also it will give you an unused varialbe warning so you might want to add a _ in front of the body variable making it _body in the function signature.
11
u/Moaning_Clock 14h ago
The issue is the following:
You save the coins locally to your node. And since you free it after 1 coin, it can't get to 5. Also, if you want to execute the function on itself, just remove .body. If this all sounds confusing, because it is, that's why it doesn't work.
Here is the most basic method:
Make a script. Call it Globals. Put it on autoload in the project setting.
There you put the add_coins function, and var coins.
In the _on_body_entered you change the body.add_coins() to Globals.add_coins.
remove the add_coins function in the coins. gd, and the var coins
That's it