0 votes

Hi,

I'm trying to write data from my game to a txt file. Here's the example code:

func collect_data():
    var data = "my data"
    var file = File.new()
    file.open("res://data.txt", File.WRITE)
    file.seek_end()
    file.store_string(data)
    file.close()

This is essentially added to a button and activated when pressed, the button generates new data every time. Everything is working fine, I'm getting the data correct it's writing to file as well. The problem is whenever the button is pressed the current data overwrites the previous one.

I would like to keep all the data the button presses, but I can't figure out how to do that. I should mention that I'm using Godot to create a game for research purposes (PhD student) hence why I keep mentioning 'data' lol. The data is basically elements in the game state.

in Engine by (39 points)

From what I've seen, you may have to first read the contents of the file (when it does have data), append the data to an object which contains the file's data, and then write the object to the original file.

Then again, I have rarely used the file capabilities of the engine, so maybe there is a way to append the data to a current file without having a temporary variable.

1 Answer

0 votes
Best answer

Open with File.READ_WRITE:

file.open("res://data.txt", File.READ_WRITE)
by (3,957 points)
selected by

Aaah that did the trick, thanks!

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.

Categories