How to save and load only player position (in 2D game)

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

Hey I’m a bit new to Godot, but not to new to the point I don’t know anything about it. I’m making a 2D rpg sort of game and need to save and load only the player’s position for now. Can I have a bit of sample code?

:bust_in_silhouette: Reply From: kidscancode

I presume by “save and load” you mean store in a file and load at runtime?

If so, the File class has you covered:

There is example code on the docs page. Using store_var() you can store the Vector2 position and get_var() to retrieve it.

For more detailed examples, see here:
http://godotrecipes.com/basics/file_io/

So I followed your website and wrote this code in the player’s code:

class_name CustomObject
var Player_file = "user://Player_file"
var Playerposition = Vector2()
var c = CustomObject.new()

func _ready():
   pass

func save_to_file():
var file = File.new()
file.open(Player_file, File.WRITE)
file.store_var(c, true)
file.close()


func load_from_file():
var file = File.new()
if file.file_exists(Player_file):
	file.open(Player_file, File.READ)
	c = file.get_var(true)
	file.close()

func _physics_process(_delta):
if Input.is_key_pressed(KEY_O):
	save_to_file()
if Input.is_key_pressed(KEY_P):
	load_from_file()

(yes, there are indents)
But there is an error. It states “Using own name in class file is not allowed.”
Any help here?

Progamer27 | 2020-04-29 16:26

You definitely shouldn’t be doing this in _physics_process() like that. No matter how quickly you tap the key, it’s going to to be pressed for multiple frames. I recommend looking at Input examples — Godot Engine (3.2) documentation in English to understand how to properly handle inputs.

kidscancode | 2020-04-29 21:21

ok, so I fixed the _physics_process() and replaced it with _Input(). Now about the error, how can that be fixed?

Progamer27 | 2020-04-29 22:24

This is your problem:

class_name CustomObject
var c = CustomObject.new()

You can’t declare that this file is the CustomObject class, and then also make a new object of that class here.

kidscancode | 2020-04-29 22:28

I’m not sure if I understand. Does those lines of code have to go under something? I have them written in the code above.

Progamer27 | 2020-04-29 22:36

What do you even need a custom object for? That portion of my tutorial was to show that if you had a custom object defined in another file, you could save it. Just remove both of those lines.

kidscancode | 2020-04-29 22:38

slight misunderstanding by me. I’ll see what I can do.

Progamer27 | 2020-04-29 22:40

Ok, so I went through the code a bit, and still nothing. Accept, no errors this time. I will show you the code and explain to you my thought process.

var Player_file = "user://Player_file"

func _ready():
pass

func save_to_file():
var file = File.new()
file.open(Player_file, File.WRITE)
file.store_var(Vector2())
file.close()

func load_from_file():
var file = File.new()
if file.file_exists(Player_file):
	file.get_var(Vector2())
	file.close()

func _Input(event):
if event.is_key_pressed(KEY_O):
	save_to_file()
if event.is_key_pressed(KEY_P):
	load_from_file()

So my thought process here is that when the O key is pressed, it calls to back the function. It Opens the Player_file, and writes the Vector2() position. Then it closes the file. Then when P is pressed, it checks to see if Player_file exists. If it does, It will read the Vector2() position and set the player backed to the saved position. I know something is wrong, but I don’t know what. Sorry that I slamming you with questions.

Progamer27 | 2020-04-29 22:56

A couple of things.

  1. Your key presses are being ignored because _Input() should be spelled _input()

  2. file.store_var(Vector2()). This is not storing the position, because you didn’t mention the position at all. It’s storing an empty vector, (0, 0). file.store_var(position) is what you want.

  3. file.get_var(Vector2()) - this is completely backwards and will give you an error once you fix #1. get_var() retrieves the data from the file, so nothing goes in the parentheses. You have to put the data into a variable. position = get_var().

Now if you fix all that, you can run and it won’t look like anything happened unless you actually move your character between saving and loading. I have no idea if/how you’re doing that. Is this code on your player? If not, where is it and how does it access the player’s position? Have you made a player that moves around? If not, you should be doing that before worrying about this.

kidscancode | 2020-04-29 23:02

Ok I did it! Thank you so much for your help!

Progamer27 | 2020-04-29 23:29

  1. file.get_var(Vector2()) - this is completely backwards and will
    give you an error once you fix #1. get_var() retrieves the data from
    the file, so nothing goes in the parentheses. You have to put the data
    into a variable. position = get_var().

position = get_var() gives error .
can you write exactly what to change instead of file.get_var(Vector2()) . ?

Sha6er | 2020-09-09 09:13

I did, you quoted it:

position = file.get_var()

kidscancode | 2020-09-09 15:18