Player position won´t update after saving and loading the game (but it does!?)

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

Hi!

So i´ve run into a very weird situation. I successfully saved the position of my character (via File class) and according to my “print()-debugging” the loaded position vector is also correct, BUT:

The actual player position does not update in the in-game world!?
(the scene updates correctly)

Anyway here is the code of my Save autoload (“data” is an empty dictionary):

func save_game() -> void:
data.player_pos = get_tree().current_scene.find_node("Player").global_position
data.scene = get_tree().current_scene.filename as String

var file = File.new()
file.open(save_file, File.WRITE)
file.store_var(data)
file.close()

func load_game() -> void:
var file = File.new()
file.open(save_file, File.READ)
data = file.get_var()
file.close()

get_tree().change_scene(data.scene)
for member in get_tree().get_nodes_in_group("saveables"):
	member.refresh()

And here is the relevant Player code (this function gets called correctly!):

func refresh() -> void:
self.global_position = Save.data.player_pos

Again, the refresh()-function gets called 100% and the values of both self.global_position and Save.data.player_pos are 100% what they should be, but in the actual game world, my character won´t update its position (i also tried just normal position instead of global_position).

Thank you very much for making it this far!

EDIT: Trimmed some fat off the code snippets.

UPDATE: So i think i found the cause. The player changes its position but it is then reset when the current scene is “ready”, it also contains the player with a default position.

I´ve updated my load_game() function to this:

func load_game() -> void:
var file = File.new()
file.open(save_file, File.READ)
data = file.get_var()
file.close()

get_tree().change_scene(data.scene)
yield(get_tree().create_timer(0.1), "timeout") # THIS IS NEW!!!
for member in get_tree().get_nodes_in_group("saveables"):
	member.refresh()

And it “works” but it´s a very ugly work around where the character teleports at the start… I also tried yielding until the parent scene signals a “ready” but it doesn´t work…

:bust_in_silhouette: Reply From: Inces

Very weird :slight_smile: It must be illusion of sorts :slight_smile:

Maybe there are other nodes, which are responsible for visual representation of characters in space, and you didn’t save/load them ? Like camera ? Or is there some issue in code, that makes only characters sprites be left behind ?

Hi!

I thought that as well, but its really is the owner.position of my whole player scene.

So what i tried now was putting my player position in the _physics_process function and i have a new hint:

(170, 245)
(305.77832, 250.679733)
(305.77832, 250.679733)
(170, 245)
(170, 245)
(…)

It actually updates shortly to the position i want it to, but then it changes to the old position again! I´m looking for where this happens but i haven´t found it yet, hmm…

Oian | 2021-02-25 22:05

You could eventually move default positioning to init instead of ready :slight_smile:

Inces | 2021-02-26 08:25

:bust_in_silhouette: Reply From: Whalesstate

a good work around is to add an autoload script to the game and when the game loads it changes the player_pos variable in this script , and when the player is ready it should load it’s position from the autoload script instead of waiting for a function to update it’s position , also you can keep the save and load inside the autoload script because it runs first before any other script in the game