How to store player health variable

Godot Version



<How do you store the player health by using autoload so that every time when the scene is changed, the player health wont reset.

@export var health_max := 100
var health := health_max

func calculate_health(amount: int):
	print("player take damage")
	health = max(0, health - amount)
	if health == 0:
		state_in_death = true

I can’t help with autoloads since I’ve hwrdly used them, but I’ve solved this problem using a resource, since those can be quickly saved as files.

I did this according to HeartBeast’s Action RPG tutorial series. I can’t remember the exact implementation, and it was for Godot 3 anyway.

This would be a guess here. But make a database class and create a simple place to store information and use that to call functions or get/set values directly.


class_name Database

static var Player_Health:int = 100

Example One:

extends Node

var Health:int

func _ready():
	Health = Database.Player_Health

func Update_Health():
	Database.Player_Health = Health

Example Two:

extends Node

var Health : int :
	get : return Database.Player_Health
	set(value) : Database.Player_Health = value