Saving Variables via Packaging

Godot Version

4.4

Question

Hello there! I’m working on a checkpoint system, which works via packing the current scene, and loading it up whenever the player dies. However, the variables of all the objects inside the loaded scene are not saved. For example, dead enemies come back to life and broken doors are un-broken. How would I go along with saving the variables of these separate scenes? Below is my saving and loading script, as well as the scene tree.

func _process(delta):
	if Input.is_action_just_pressed("1"):
		var node_to_save = self
		var scene = PackedScene.new()
		scene.pack(node_to_save)
		ResourceSaver.save(scene, "res://levelCheckpoint.tscn")
	
	if Input.is_action_just_pressed("2"):
		if !FileAccess.file_exists("res://levelCheckpoint.tscn"):
			get_tree().reload_current_scene()
		else:
			var new_scene = ResourceLoader.load("res://levelCheckpoint.tscn").instantiate()
			get_tree().current_scene.queue_free()
			get_tree().root.add_child(new_scene)
			get_tree().current_scene = new_scene

An example enemy script as well:

extends CharacterBody3D

var player = null
var health = 3
var knockback_velocity: Vector3
var yesdead = false

@export var player_path : NodePath

const SPEED = 13.0

@onready var anim = $AnimatedSprite3D
@onready var shield = $CSGSphere3D
@onready var nav_agent = $NavigationAgent3D

signal dead

# Called when the node enters the scene tree for the first time.
func _ready():
	player = get_node(player_path)
	anim.play("default")
	shield.visible = false
	set_process(false)
	$Bulletbox/CollisionShape3D.set_deferred("disabled", true)


# Called every frame. 'delta' is the elapsed time since the previous frame.
func _process(_delta):
	velocity = Vector3.ZERO
	nav_agent.set_target_position(player.global_transform.origin)
	var next_nav_point = nav_agent.get_next_path_position()
	velocity = (next_nav_point - global_transform.origin).normalized() * SPEED
	
	look_at(Vector3(player.global_position.x, global_position.y, player.global_position.z), Vector3.UP)
	
	if knockback_velocity:
		velocity = knockback_velocity
	
	move_and_slide()


func _on_bulletbox_body_hit(dam):
	health -= dam
	if health <= 0 and yesdead == false:
		yesdead = true
		$Bulletbox/CollisionShape3D.set_deferred("disabled", true)
		$CollisionShape3D.set_deferred("disabled", true)
		dead.emit()
		set_process(false)
		$Walk.stop()
		$AnimationPlayer.play("Death")
		$Hitbox/CollisionShape3D.set_deferred("disabled", true)

using res:// paths won’t work once you export the game, res:// becomes read-only so no data may be saved. You could try user://.

Your variables may need to be @exports for them to save as part of a packed scene.

thanks a ton! this worked, yes.