An easier way of restarting

Godot Version

4.6.2

Question

I am working on my first game and am wondering if there is an easier way of resetting autoloads than setting them back to their defaults?

extends Button

func _on_pressed() → void:
GameState.restarting = false
GameState.enemy_1_positions = Vector2(41, -79)
GameState.enemy_2_positions = Vector2(-26, -79)
Global.health = 100
GameState.sprint = 100
GameState.money = 0
GameState.enemy_limit = 5
GameState.heal_price = 15
GameState.waves = 1
GameState.dagger_price = 15
GameState.dagger_bought = false
GameState.dagger_upgrade = 0
GameState.hammer_bought = false
GameState.hammer_price = 20
GameState.hammer_upgrade = 0
GameState.melee_bought = false
GameState.ranged_bought = false
GameState.base_weapon = true
GameState.dragon_health = 50
GameState.dragon_max_hp = 50
GameState.dragon_drop = 30
get_tree().reload_current_scene()

func _ready():
if not GameState.restarting:
get_tree().change_scene_to_file(“res://main_menu.tscn”)
GameState.restarting = true
else:
pass

why do you need them as autoloads if they shouldnt be persistent? Wouldn’t it make more sense to just make them part of the scene and whenever it reloads they get set to the base values again

They need to be persistent because they are for the shop. I need them to reset when the player loses.

okay i see. Then i would probably make a function to reset all values and maybe add constants for the default values at the start of the script.

You could also just make a variables/constants script which stores all the default values, so you dont have to go change it in the reset function

Thank you!

You’re welcome! :slight_smile: