settings.gd 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940
  1. extends Node
  2. var render_distance = 7
  3. var fog_enabled = true
  4. var fog_distance = 32.0 # Not saved, only used during runtime.
  5. var world_type = 0 # Not saved, only used during runtime.
  6. var _save_path = "user://settings.json"
  7. @warning_ignore("unused_private_class_variable")
  8. var _loaded = false
  9. func _enter_tree():
  10. if Settings._loaded:
  11. printerr("Error: Settings is an AutoLoad singleton and it shouldn't be instanced elsewhere.")
  12. printerr("Please delete the instance at: " + String(get_path()))
  13. else:
  14. Settings._loaded = true
  15. if FileAccess.file_exists(_save_path):
  16. var file = FileAccess.open(_save_path, FileAccess.READ)
  17. while file.get_position() < file.get_length():
  18. # Get the saved dictionary from the next line in the save file
  19. var json = JSON.new()
  20. json.parse(file.get_line())
  21. var data = json.get_data()
  22. render_distance = data["render_distance"]
  23. fog_enabled = data["fog_enabled"]
  24. else:
  25. save_settings()
  26. func save_settings():
  27. var file = FileAccess.open(_save_path, FileAccess.WRITE)
  28. var data = {
  29. "render_distance": render_distance,
  30. "fog_enabled": fog_enabled,
  31. }
  32. file.store_line(JSON.stringify(data))