settings.gd 896 B

1234567891011121314151617181920212223242526272829303132
  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. func _enter_tree() -> void:
  8. if FileAccess.file_exists(_save_path):
  9. var file := FileAccess.open(_save_path, FileAccess.READ)
  10. while file.get_position() < file.get_length():
  11. # Get the saved dictionary from the next line in the save file
  12. var json := JSON.new()
  13. json.parse(file.get_line())
  14. var data: Dictionary = json.get_data()
  15. render_distance = data["render_distance"]
  16. fog_enabled = data["fog_enabled"]
  17. else:
  18. save_settings()
  19. func save_settings() -> void:
  20. var file := FileAccess.open(_save_path, FileAccess.WRITE)
  21. var data := {
  22. "render_distance": render_distance,
  23. "fog_enabled": fog_enabled,
  24. }
  25. file.store_line(JSON.stringify(data))