+1 vote

I have a control script that controls my game, it is a autoload script, i want to know how i can save the data in my control script and load it when continued.
My control script is not attached to any scene or node

in Engine by (392 points)

2 Answers

0 votes

I'm assuming that you want to store the data locally?
For storage you could use JSON (you can read about it here) or perhaps a local database.

by (120 points)

Yeah ive read the docs but i how do i save the data from a script that has no node or scene because the docs talk about groupping and you cant group a stand alone script

0 votes

Just use file's read/write operations at your convenience (full docs or in editor's Search Help).
In my simplest clicker game, i need to save only top result and using this code:

func save_result():
var settings = File.new()
if settings.open(app.SETTINGS_FILE, File.WRITE) != 0:
    print("Error opening file")
    return
settings.store_16(app.result_time)
settings.close()

func load_result():
var settings = File.new()
if !settings.file_exists(app.SETTINGS_FILE):
    print("settings.save not exist")
    return MAX_VALUE
if settings.open(app.SETTINGS_FILE, File.READ) != 0:
    print("Error opening file")
    return MAX_VALUE
var top_result_time = settings.get_16()
settings.close()
return top_result_time

where app - autoload singleton not attached to any scene or node.
You can read/write multiple data as JSON (as say Christoffer Schindel), as raw data, bytes, strings, etc, it's depends on your needs.

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