extends Object class_name TopicData signal topic_added(topic) signal topic_inserted(topic, position) const FILE_NAME = "topic_list.dat" var dirty_flag = false var loaded_topics = [] func add_topic(content, type): # On start-up. var topic = Topic.new() topic.content = content topic.type = type loaded_topics.append(topic) emit_signal("topic_added", topic) func insert_topic(position: int): var topic = Topic.new() loaded_topics.append(topic) dirty_flag = true emit_signal("topic_inserted", topic, position) func sort_topics(): loaded_topics.sort_custom(self, "_compare_topic_index") func _compare_topic_index(a, b): return a.associated_node.parent.get_index() < b.associated_node.parent.get_index() func load_data(): var file = File.new() if not file.file_exists("user://" + FILE_NAME): return file.open("user://" + FILE_NAME, File.READ) var json_string = file.get_as_text() if validate_json(json_string): printerr("\"" + FILE_NAME + "\" was found, but is corrupted.") return var topic_data_list = parse_json(json_string) for topic_data in topic_data_list: add_topic( topic_data["content"], int(topic_data["type"]) ) func save_data(): if dirty_flag: sort_topics() # Important before saving, to remain the order. var topic_data_list = [] for topic in loaded_topics: var topic_data = { "content" : topic.content, "type" : topic.type } topic_data_list.append(topic_data) var json_string = to_json(topic_data_list) var file = File.new() file.open("user://" + FILE_NAME, File.WRITE) file.store_string(json_string) file.close() # dirty_flag needs to be set to false after successful save! dirty_flag = false