Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- extends Node
- #The Data System used is designed for my 3D Diary application.
- #Data setup
- const SAVE_DIR = "user://saves/" #This is useful for HTML5 as well.
- # Called when the node enters the scene tree for the first time.
- func _ready():
- pass # Replace with function body.
- func _input(event):
- if event is InputEventKey:
- if event.scancode == KEY_F11 and event.echo == false and event.pressed:
- OS.window_fullscreen = !OS.window_fullscreen
- func _ensure_directory_exists(path: String) -> void:
- var dir := Directory.new()
- if not dir.dir_exists(path):
- var __ = dir.make_dir_recursive(path)
- #Loads a game, or can be used to load old data so it can insert with new data.
- func _load_or_default(path: String, default: Dictionary) -> Dictionary:
- var file := File.new()
- if not file.file_exists(path):
- return default
- var error := file.open(path, File.READ)
- if error == OK:
- #var data = file.get_var()
- #file.close()
- #2. Create a file
- var save_file = File.new()
- save_file.open(path,File.READ)
- var data = parse_json(save_file.get_as_text())
- return data
- else:
- print("ERROR LOADING: %s" % error)
- return default
- func save(SAVE_PATH, index, value):
- _ensure_directory_exists(SAVE_DIR)
- var original_data = _load_or_default(SAVE_PATH, {"default_data" : "to prevent any data glitches or data not found error if data is empty at start. That's why this note here exist."})
- var data_dict : Dictionary = {}
- data_dict[index] = value
- var overrides : Dictionary = data_dict
- var new_data := merge(original_data, overrides)
- #2. Create a file
- var save_file = File.new()
- save_file.open(SAVE_PATH,File.WRITE)
- #3. Serialize the data dictionary to JSON
- var save_string = JSON.print(new_data, "\t")
- save_file.store_line(save_string)#Godot 3 Code (based from another sample from data.gd)
- #4. Write the JSON to the file and save to disk
- save_file.close()
- # Put this in some Dictionary helper class.
- static func merge(left: Dictionary, right: Dictionary) -> Dictionary:
- var merged := {}
- for key in left.keys():
- merged[key] = left[key]
- print(merged[key])
- for key in right.keys():
- merged[key] = right[key]
- return merged
- #Data for objects, items, furnitures.
- func save__data_style1(SAVE_PATH, value):
- _ensure_directory_exists(SAVE_DIR)
- var original_data = _load_or_default(SAVE_PATH, {"default_data" : "to prevent any data glitches or data not found error if data is empty at start. That's why this note here exist."})
- #print(original_data)
- var data_dict : Dictionary = {}
- #data_dict["data_style1"][selectedObject][index] = value
- data_dict = value
- var overrides : Dictionary = data_dict
- var new_data := merge1(original_data, overrides)
- #2. Create a file
- var save_file = File.new()
- save_file.open(SAVE_PATH,File.WRITE)
- #3. Serialize the data dictionary to JSON
- var save_string = JSON.print(new_data, "\t")
- save_file.store_line(save_string)#Godot 3 Code (based from another sample from data.gd)
- #4. Write the JSON to the file and save to disk
- save_file.close()
- # Put this in some Dictionary helper class.
- static func merge1(left: Dictionary, right: Dictionary) -> Dictionary:
- var merged := {}
- for key in left.keys():
- merged[key] = left[key]
- for key in right.keys():
- if key != "data_style1":
- merged[key] = right[key]
- else:
- #print(right)
- for key1 in right[key].keys():
- merged[key][key1] = right[key][key1]
- #for key2 in right[key][key1].keys():
- # merged[key][key1][key2] = right[key][key1][key2]
- return merged
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement