0 votes

Hello Godot community, my variable that I created isn't storing the updated value. I have made a variable death_count with value 0 and it s supposed to increase everytime my player health reaches 0, but it's always stuck on 1 and I don't know why it is happening.
Take a look at my dead function :

func dead(): 
    playerhp-=1
    if playerhp==0:
        death_count +=1 // It's not getting incremented
        print("player with 0 health")
        is_dead=true
        if emit == false:
            $Particles2D.emitting = true
            #emit = true
        $Sprite.play("PlayerDead")
        Engine.time_scale = 0.2
        motion=Vector2(0,0)
        $CollisionShape2D.disabled = true
        print("Dead Function")
        $Timer.start()
asked Jul 3 in Projects by Scavex (194 points)

1 Answer

0 votes

Hey,

what do you mean with

it's always stuck on 1

Could it be that you never go to playerhp == 0 twice?
Then you could just if playerhp <= 0:

answered Jul 3 by Krippi (104 points)

I figured out that the number isn't increasing because I am usingget_tree().reload_current_scene()when my player dies. Do you have any idea how to increment death_countwith this ? Seems like the variable is not able to store it's previous information when the scene gets reloaded.

You can use Global Variables to store the data.
In godot u can use Singletons
https://docs.godotengine.org/de/stable/getting_started/step_by_step/singletons_autoload.html

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.