Is it good practice to pass variables between scenes this way?

Godot Version

4.2.2

Question

I was wondering if, to pass variables from a scene (in this case, player.tscn) to another one (hud.tscn) it would be a good practice to do something like this:

autoload.gd

var playerMAX_STAMINA
var playerStamina

player.gd

const MAX_STAMINA : float = 10
var stamina : float = 10

func _ready():
	Autoload.playerMAX_STAMINA = MAX_STAMINA
	Autoload.playerStamina = stamina

I tried that to show the player’s stamina in the HUD of my game and it worked perfectly, but I wondered if is it really a good practice to do it this way, and/or if it’s going to slow performance.

Seems like a unnecessary copy, couldn’t you use Autoload.playerStamina everywhere need be, and remove var stamina: float = 10, similarly with max stamina.

Good point, do you think that for several scenes it is better to save the player statistics (such as stamina) in an autoload instead of in the player scene? (To make them global)

Depends on the game, maybe stamina specifically can be private information to the player. Personally, I find autoload can be avoided for most variables, gold/money is the most common autoload I use.

1 Like

thank you, I’ll have that in mind and use Autoloads to store that info only if necessary (in my case I think it is because of the HUD displaying the stamina)

@BrodaCode Take a look at

in particular its section on Managing shared functionality or data

thanks, i’ll check it out

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.