The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

+1 vote

Now here comes my 4. saving problem...

As a result of another question:
I got this code for a simple saving system with two variables:

var level = 1
var leben = 1

func _ready():

func load_level():
    var save_file =
    if not save_file.file_exists("user://"):
        return"user://", File.READ)
    level = int(save_file.get_line())
    leben = int(save_file.get_line())

func save_level():
    var save_file ="user://", File.WRITE)

Now I changes the level variable into an array:

var level = [1,0,0,0,0,0]

What things must be changed, to save and read an array, because now, I think it, would convert the array into one int value and it wouldnt work any more.
Thanks for answers!

EDIT: I solved it by wriding every index of the array in one line of the saving file with a for function.

in Engine by (378 points)
edited by

2 Answers

+4 votes
Best answer

What i would do, is use JSON.

var data = {
'level': 1,
'somethingelse': 2

func save():
 # open file

func load():
 # open file
 data = JSON.parse(file.get_line())

This way you don't have to reimplement everything if you add a new variable.

by (267 points)
selected by

My question was more, how I can save arrays and not how I can save normal variables better. But thanks for the tip!

Oh, yeah, you can add your array as a field in the json, like {'arr': [1, 2, 3]} and it will still get serialized.

+2 votes

Did you try using convert method in GDScript?

level = convert(save_file.get_line(), 19)

19 is array in Variant.Type

by (113 points)

I changed this line:


to this line:

level = convert(save_file.get_line(), 19)

as you said, but I got this:

Invalid call. Nonexistent built-in function 'convert'.

How can I fix that?

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 Frequently asked questions and 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 [email protected] with your username.