If you are looking to store the file on the device running the game, then you can do:
var data = {
# your data goes here
}
var file = File.new()
var filepath = "user://data.json" # or whatever you want to call it
var json_string = to_json(data)
file.open(filepath, File.WRITE)
file.store_string(json_string)
file.close()
if you need to locate the user://
directory of whatever system you're on, you can use OS.get_user_data_dir()
Sorry, I forgot about the other part of your question. This should work I think, however it is untested so might need some tweaking.
# EventLogger.gd
extends Node
const SAVE_INTERVAL = 10
const FILE_PATH = 'user://events.json'
var events: Array = []
var dirty: bool = false
var timer: Timer = Timer.new()
func _ready():
timer.wait_time = SAVE_INTERVAL
timer.one_shot = false
timer.autostart = true
timer.connect("timeout", self, "save")
func log_event(event_type: String, event_data: Dictionary):
var event : Dictionary = {
'event': event_type,
'data': event_data,
};
events.append(events)
dirty = true
func save():
# if we haven't logged any new events, don't bother saving the data
if not dirty:
return
var file = File.new()
file.open(FILE_PATH, File.WRITE)
file.store_string(to_json(events))
file.close()
dirty = false
The main idea is that you add events to an array, which periodically get saved to file. I don't know how often you'll be logging events, so you probably don't want to write to file evey single time, so I have added a timer to do it every so often. You'll want to register this as an autoloaded script. Then from any other script, you can do:
func _on_dragged(block):
EventLogger.log_event('block_added_to_board', block.position)
Of course, you'll have to set up the function(s) that will call this (not _on_dragged(block)
), I don't know what your setup is like.