0 votes

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?

asked Apr 28 in Projects by Progamer27 (14 points)

1 Answer

0 votes

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

If so, the File class has you covered:
https://docs.godotengine.org/en/3.2/classes/class_file.html

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/

answered Apr 28 by kidscancode (17,762 points)

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 Playerfile, and writes the Vector2() position. Then it closes the file. Then when P is pressed, it checks to see if Playerfile 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.

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.

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

3) file.getvar(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 = getvar() gives error .
can you write exactly what to change instead of file.get
var(Vector2()) . ?

I did, you quoted it:

position = file.get_var()
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.