Attention | Topic was automatically imported from the old Question2Answer platform. | |
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…