Save.gd 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. extends Node
  2. const SAVE_PATH = "res://save.json"
  3. var _settings = {}
  4. #var nodes_to_save = []
  5. func _ready():
  6. # Called when the node is added to the scene for the first time.
  7. # Initialization here
  8. pass
  9. #fixme: better use node_path
  10. func save_node_data(node):
  11. var save_dict = {}
  12. if !node.is_queued_for_deletion( ):
  13. save_dict[node.get_path()] = node.save()
  14. var save_file = File.new()
  15. var path = "res://Saves/" + str(node.name) + ".json"
  16. save_file.open(path, File.WRITE)
  17. save_file.store_line(to_json(save_dict))
  18. save_file.close()
  19. func save_node_data_by_name(node, node_name):
  20. var save_dict = {}
  21. if !node.is_queued_for_deletion( ):
  22. save_dict[node.get_path()] = node.save()
  23. var save_file = File.new()
  24. var path = "res://Saves/" + node_name + ".json"
  25. save_file.open(path, File.WRITE)
  26. save_file.store_line(to_json(save_dict))
  27. save_file.close()
  28. func get_node_data_by_name(node_name):
  29. #print ("trying to find node data!!!")
  30. var save_file = File.new()
  31. var path = "res://Saves/" + node_name + ".json"
  32. if not save_file.file_exists(path):
  33. #print ("save path not found: " + str(path))
  34. return
  35. save_file.open(path, File.READ)
  36. var result_json = JSON.parse(save_file.get_as_text())
  37. if result_json.error == OK: # If parse OK
  38. var data = result_json.result
  39. #print(data)
  40. return data
  41. else: # If parse has errors
  42. print("Error: ", result_json.error)
  43. print("Error Line: ", result_json.error_line)
  44. print("Error String: ", result_json.error_string)
  45. return