Godot Version
V4.2.1
Question
I exported the checkpoints array in player.gd and set it up in main.tscn. Whenever the player passes through one of the checkpoints, the save()
function is called within checkpoint()
. When the player dies, load_data()
is triggered. However, when the player dies, the debugger points to $player.global_position = file.get_var($player.global_position)
in load_data()
. Additionally, I’m getting the following error: save(): Node not found: "player" (relative to "/root/Game")
.
player.gd:
extends CharacterBody2D
@export_category("Exports")
@export_group("Checkpoints")
@export var checkpoints = [] #list of vector2
func die():
hit = true
velocity.y = -700
collision_mask = 32
collision_layer = 32
main.gd:
extends Node2D
var save_path = "user://newGame.save"
func _physics_process(_delta):
Saw()
SpikedBall()
RockHead()
for i in $Player.get_slide_collision_count():
var collision = $Player.get_slide_collision(i)
collision = collision.get_collider()
#print("Collided with: %s" % collision.name)
if collision.name == "Trampoline":
Trampoline()
elif collision.name.begins_with("Spikes"):
Spikes()
$Player/MetalHitSound.play()
elif collision.name.begins_with("Saw"):
$Player.die()
load_data()
$Player/MetalHitSound.play()
elif collision.name.begins_with("Fire"):
var Fire = collision
Fire.FireHit = true
if Fire.FireHit == true and Fire.FireOn == true:
$Player.die()
load_data()
elif collision.name.begins_with("Box"):
var Box = collision
Box.BoxHit = true
$Player.velocity.y = -700
elif collision.name.begins_with("SpikedBall"):
$Player.die()
load_data()
$Player/MetalHitSound.play()
checkpoint() #save at checkpoints
func checkpoint():
for check in $Player.checkpoints:
if $Player.global_position.x >= check.x:
save()
func save():
if $player:
var file = FileAccess.open(save_path, FileAccess.WRITE)
file.store_var($player.global_position)
file.store_var($player.collision_mask)
file.store_var($player.collision_layer)
func load_data():
if $Player:
if FileAccess.file_exists(save_path):
var file = FileAccess.open(save_path, FileAccess.READ)
$player.global_position = file.get_var($player.global_position) #Debugger pointing at this
$player.collision_mask = file.get_var($player.collision_mask)
$player.collision_layer = file.get_var($player.collision_layer)
else:
print("no data saved")