0 votes

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.globalposition and Save.data.playerpos 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..

Godot version 3.2.3
in Engine by (40 points)
edited by

2 Answers

+1 vote

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

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 ?

by (1,544 points)

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 physicsprocess 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...

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

+1 vote

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

by (274 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.