How to reset variables in Global script?

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

I’m making it so that if the furnace life goes to zero the scene will reset. But my problem is that if you put a variable in a Global script, the variable will not reset if you reset the scene. Are there any better way to to this?

func _process(_delta): if Global.Furnace_Life == 0: get_tree().reload_current_scene()

:bust_in_silhouette: Reply From: Inces

Autoload Global script is exactly for situations of keeping variables, that You want to keep unchanged when scenes are replaced. Like difficulty level or players statistics, or graphic options or saved states of game. You should have kept furnace hp in resettable furnace script. Just leave Global out of this and your problem is gone.

Ty for the suggestion. Yes I tried that, but I wanna use the variable in other scripts. Is there any other way of using variables in other scripts?

Jan Gabriel | 2022-04-23 19:07

Why would You need to keep this variable in other scripts ? Maybe You want to have global variable maxhp, but You should keep local hp in local scripts.

Inces | 2022-04-24 13:58

Ok, but if we return to the original question - how to reset variables in global scripts? Lets say I have level1, level2, character and menu scenes - character can collect coins in level1, at the end the level1 is changed to level2 and I want to keep number of coins and continue collecting them (so I have to make Global script for that). If character dies during level1 or level2 I want to return to menu scene where number of coins obviously return to original value - zero. How can I accomplish that? I mean is there any way to reset that global script or do I have to set manually every variable to original value (like setting that in menu scene idk)?

JokerSimpers | 2023-04-28 11:12

Autoload script behaves just like any other script. It can have properties, that are accessed and changed. The main difference is, that Autoload script is not freed when level 1 is swapped into level 2. So You just need to do something as simple as :

on_coin_collected :
      Autoload.coins += 1
on_player_death:
      Autoload.coins = 0

Also, Autoload can be reached from everywhere thorughout your code, theres is no need to create any hardcode reference to it. So You can call it from levels, player or menus.

Inces | 2023-04-28 13:43

Yeah, I was thinking about that I just thought that faster would be reseting autoload to original state nvm, e.g. for 100 variables still not big deal. Thanks

JokerSimpers | 2023-04-28 19:03