Why my variable isn't storing the updated value ?

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

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()
:bust_in_silhouette: Reply From: Krippi

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:

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.

Scavex | 2020-07-03 16:25

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

Krippi | 2020-07-08 08:01