Before we talk about what’s going on with your player position, we should talk about naming conventions. It took some time to parse your code. Save indicates saving something, but you also apparently are using it for loading? At first it looked like you were saving the data when you load it. I’d recommend you come up with a different name for Save such as SaveLoad or Disk.
Here’s some modified code. First, I separated the loading of the data from the testing and using of the data. Second, I reduced the number of calls to the data. A retrieval of data into a variable takes virtually the same amount of time as seeing if it’s there. In this case, if it’s there you want to use it, so just load it.
When data is loaded, if it doesn’t exist, the vaariable will be assigned the value of null, and an if statement interprets that as false. So if the pos_x
isn’t null, it will be assigned. Otherwise, it won’t. This is a good way of handling nulls so they don’t cause you problems later.
You’ll also notice there’s only one print statement. This is because once you assign the values to a variable and print that out - you know the value of the variable isn’t going to change. Plus it can be used to assign to your player.
You’ll also notice my use of constants (keyword const
) at the top of the file. This is to eliminate what are know as “magic strings”. Whenever a string (or number) doesn’t change, make it const. That prevents a lot of frustrating typo errors that are hard to track down.
The load_player_position()
name makes it clear what this script is doing, regardless of the named autoload Save and makes your code easier to read.
The last thing to mention is I added a call to what I think is your call to load the data. You may be doing this somewhere else, but for now - this ensures the data is loaded.
const SAVED_PLAYER_POSITION_X = "player_global_positionX"
const SAVED_PLAYER_POSITION_Y = "player_global_positionY"
func _ready():
var loaded_position = load_player_position()
if loaded_position != Vector.ZERO:
player.global_position = loaded_position
else:
print("Position data not found in save file.")
func load_player_position() -> Vector2:
Save.load_game() #We want to make sure the data is loaded. If you are doing this somewhere else, you do not need it her.
var return_vector := Vector2.ZERO #This is our error vector. If it's possible that (0,0) is a valid position, use Vector2.INF instead
var pos_x = Save.saved_data[SAVED_PLAYER_POSITION_X]
if pos_x:
return_vector.x = pos_x
var pos_y = Save.saved_data[SAVED_PLAYER_POSITION_Y]
if pos_y:
return_vector.y = pos_y
print("Retrieved position X = %s and Y = %s" % [return_vector.x, return_vector.y])
return return_vector
So, now while you said your character is gettign teleported wildly, you didn’t say where. So here’s some questions for you:
- What was the player’s position when you saved it?
- What was the player’s global_position when you saved it?
- What was the player’s position before the load?
- What was the player’s global_position before the load?
- What was the player’s position after the load?
- What was the player’s global_position after the load?
- Has the player changed order or parent in the node hierarchy so that the relative position of the player might have changed?
- Are you sure you are saving global_position and not position?
- Are you loading into the exact same level with nothing changed as when the save happened, or has anything about the level changed?