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"]

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.)

The output now is

what I need


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")


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.