0 votes

Hi! how can I save each element of an array in a new line of a text file using GDScript? I want each element of the array appears as a new line in the text file.

I am using the following code to do that, but what I'm getting is the whole array in one big line in the text file

func _ready():
    array_of_strings = ["a,a,a,a", "b,b,b,b", "c,c,c,c"]
    save(array_of_strings)

func save(array_):
    var file = File.new()
    file.open("res://Results//save_game.txt", File.WRITE)
    for i in range(array_.size()):
       file.store_line(String(array_[i])) #file.store_string(content.)
    file.close()

The output now is
[a,a,a,a][b,b,b,b][c,c,c,c]

what I need
a,a,a,a
b,b,b,b
c,c,c,c

Thanks!!

in Engine by (129 points)

1 Answer

0 votes
Best answer

Use this

func save(content):
    var file = File.new()
    if (file.open("res://Results//save_game.txt", File.WRITE)== OK):
        for i in range(content.size()):
            file.store_line(String(content[i]) + "\r")
        file.close()

from
https://godotengine.org/qa/30543/files-store_line-stores-all-lines-as-one-block-of-text

by (129 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.