/
mrFeld
/
Platformer
Обзор
Документация
Войти
/
mrFeld
/
Platformer
Код
Запросы
0
Задачи
Вики
Пакеты
0
Релизы
0
Аналитика
Безопасность
master
Script/System/SimpleSaveManager.gd
70 строк
3 KB
Neterov Vladimir
quests
19 ноя 2025, 17:36
19 ноя 2025, 17:36
ab546f8
Код
Авторство
О чём код?
extends Node class_name SimpleSaveManagerClass const SAVE_PATH := "user://save_main.json" # можно слотами: user://saves/slot_1/save.json func save_game(): var save_file = FileAccess.open("user://savegame.save", FileAccess.WRITE) var save_nodes = get_tree().get_nodes_in_group("Savable") for node in save_nodes: # Check the node is an instanced scene so it can be instanced again during load. if node.scene_file_path.is_empty(): print("persistent node '%s' is not an instanced scene, skipped" % node.name) continue # Check the node has a save function. if !node.has_method("save"): print("persistent node '%s' is missing a save() function, skipped" % node.name) continue # Call the node's save function. var node_data = node.call("save") var json_string = JSON.stringify(node_data) # Store the save dictionary as a new line in the save file. save_file.store_line(json_string) func load_game(): if not FileAccess.file_exists("user://savegame.save"): return # Error! We don't have a save to load. # We need to revert the game state so we're not cloning objects # during loading. This will vary wildly depending on the needs of a # project, so take care with this step. # For our example, we will accomplish this by deleting saveable objects. var save_nodes = get_tree().get_nodes_in_group("Savable") for i in save_nodes: i.queue_free() # Load the file line by line and process that dictionary to restore # the object it represents. var save_file = FileAccess.open("user://savegame.save", FileAccess.READ) while save_file.get_position() < save_file.get_length(): var json_string = save_file.get_line() # Creates the helper class to interact with JSON. var json = JSON.new() # Check if there is any error while parsing the JSON string, skip in case of failure. var parse_result = json.parse(json_string) if not parse_result == OK: print("JSON Parse Error: ", json.get_error_message(), " in ", json_string, " at line ", json.get_error_line()) continue # Get the data from the JSON object. var node_data = json.data # Firstly, we need to create the object and add it to the tree and set its position. var new_object = load(node_data["filename"]).instantiate() get_node(node_data["parent"]).add_child(new_object) if node_data.has("pos_x") and node_data.has("pos_y"): if new_object is Node2D: new_object.position = Vector2(node_data["pos_x"], node_data["pos_y"]) # Now we set the remaining variables. for i in node_data.keys(): if i == "filename" or i == "parent" or i == "pos_x" or i == "pos_y": continue new_object.set(i, node_data[i])