Advertisement
MarkJD68

My 3D Prototype SystemControl.gd code as of Jan 20, 2023

Jan 20th, 2023
2,240
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Node
  2.  
  3. #The Data System used is designed for my 3D Diary application.
  4.  
  5. #Data setup
  6. const SAVE_DIR = "user://saves/" #This is useful for HTML5 as well.
  7.  
  8. # Called when the node enters the scene tree for the first time.
  9. func _ready():
  10.     pass # Replace with function body.
  11.  
  12. func _input(event):
  13.     if event is InputEventKey:
  14.         if event.scancode == KEY_F11 and event.echo == false and event.pressed:
  15.             OS.window_fullscreen = !OS.window_fullscreen
  16.  
  17. func _ensure_directory_exists(path: String) -> void:
  18.     var dir := Directory.new()
  19.  
  20.     if not dir.dir_exists(path):
  21.         var __ = dir.make_dir_recursive(path)
  22.  
  23. #Loads a game, or can be used to load old data so it can insert with new data.
  24. func _load_or_default(path: String, default: Dictionary) -> Dictionary:
  25.     var file := File.new()
  26.    
  27.     if not file.file_exists(path):
  28.         return default
  29.    
  30.     var error := file.open(path, File.READ)
  31.    
  32.     if error == OK:
  33.         #var data = file.get_var()
  34.         #file.close()
  35.        
  36.         #2. Create a file
  37.         var save_file = File.new()
  38.         save_file.open(path,File.READ)
  39.         var data = parse_json(save_file.get_as_text())
  40.        
  41.         return data
  42.        
  43.     else:
  44.         print("ERROR LOADING: %s" % error)
  45.         return default
  46.  
  47. func save(SAVE_PATH, index, value):
  48.     _ensure_directory_exists(SAVE_DIR)
  49.     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."})
  50.    
  51.     var data_dict : Dictionary = {}
  52.     data_dict[index] = value
  53.    
  54.     var overrides : Dictionary = data_dict
  55.     var new_data := merge(original_data, overrides)
  56.    
  57.     #2. Create a file
  58.     var save_file = File.new()
  59.     save_file.open(SAVE_PATH,File.WRITE)
  60.    
  61.     #3. Serialize the data dictionary to JSON
  62.     var save_string = JSON.print(new_data, "\t")
  63.     save_file.store_line(save_string)#Godot 3 Code (based from another sample from data.gd)
  64.    
  65.     #4. Write the JSON to the file and save to disk
  66.     save_file.close()
  67.  
  68. # Put this in some Dictionary helper class.
  69. static func merge(left: Dictionary, right: Dictionary) -> Dictionary:
  70.     var merged := {}
  71.    
  72.     for key in left.keys():
  73.         merged[key] = left[key]
  74.         print(merged[key])
  75.    
  76.     for key in right.keys():
  77.         merged[key] = right[key]
  78.    
  79.     return merged
  80.  
  81.  
  82. #Data for objects, items, furnitures.
  83. func save__data_style1(SAVE_PATH, value):
  84.     _ensure_directory_exists(SAVE_DIR)
  85.     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."})
  86.     #print(original_data)
  87.     var data_dict : Dictionary = {}
  88.     #data_dict["data_style1"][selectedObject][index] = value
  89.     data_dict = value
  90.    
  91.     var overrides : Dictionary = data_dict
  92.     var new_data := merge1(original_data, overrides)
  93.    
  94.     #2. Create a file
  95.     var save_file = File.new()
  96.     save_file.open(SAVE_PATH,File.WRITE)
  97.  
  98.     #3. Serialize the data dictionary to JSON
  99.     var save_string = JSON.print(new_data, "\t")
  100.     save_file.store_line(save_string)#Godot 3 Code (based from another sample from data.gd)
  101.    
  102.     #4. Write the JSON to the file and save to disk
  103.     save_file.close()
  104.  
  105. # Put this in some Dictionary helper class.
  106. static func merge1(left: Dictionary, right: Dictionary) -> Dictionary:
  107.     var merged := {}
  108.    
  109.     for key in left.keys():
  110.         merged[key] = left[key]
  111.    
  112.     for key in right.keys():
  113.         if key != "data_style1":
  114.             merged[key] = right[key]
  115.         else:
  116.             #print(right)
  117.             for key1 in right[key].keys():
  118.                 merged[key][key1] = right[key][key1]
  119.                 #for key2 in right[key][key1].keys():
  120.                 #   merged[key][key1][key2] = right[key][key1][key2]
  121.    
  122.     return merged
  123.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement