Advertisement
freswinn

GODOT 3 to 4 Example: Changes between file opening, file saving, export and setget, & use of JSON

Mar 3rd, 2023 (edited)
1,851
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Some samples of changes in reading and writing files and converting JSON #
  2.  
  3.  
  4. #############
  5. #### 3.5 #### (Any line with ###### at the end will be different in the 4.0 version)
  6. #############
  7.  
  8. export var dbpath : String = DBFolder + DefaultDB setget set_dbpath ######
  9.  
  10. func check_for_settings_file(verbose : bool = false) -> bool:
  11.     var got_it = File.new().file_exists(SettingsPath) ######
  12.     if verbose: print(got_it)
  13.     return got_it
  14.  
  15.  
  16. func load_settings(verbose : bool = false):
  17.     if !check_for_settings_file():
  18.         if verbose: print("Settings.gd, load_settings || Settings file missing! Creating new settings file from default settings.")
  19.         settings = DefaultSettings
  20.         save_settings()
  21.     else:
  22.         if verbose: print("Settings.gd, load_settings || Settings file found!")
  23.         var source = File.new() ######
  24.         source.open(SettingsPath, File.READ) ######
  25.         settings = JSON.parse(source.get_as_text()).result ######
  26.         source.close() ######
  27.         if verbose: print("Settings.gd, load_settings || settings: \n" + str(settings))
  28.  
  29.  
  30. func save_settings(verbose : bool = false):
  31.     var new_file = File.new() ######
  32.     new_file.open(SettingsPath, File.WRITE) ######
  33.     new_file.store_string(to_json(settings)) ######
  34.     new_file.close()
  35.     if verbose: print("Settings.gd, save_settings || Settings file saved.")
  36.  
  37.  
  38. #############
  39. #### 4.0 ####
  40. #############
  41.  
  42. @export var dbpath : String = DBFolder + DefaultDB : set = set_dbpath
  43.  
  44. func check_for_settings_file(verbose : bool = false) -> bool:
  45.     var got_it = FileAccess.file_exists(SettingsPath)
  46.     if verbose: print(got_it)
  47.     return got_it
  48.  
  49.  
  50. func load_settings(verbose : bool = false):
  51.     if !check_for_settings_file():
  52.         if verbose: print("Settings.gd, load_settings || Settings file missing! Creating new settings file from default settings.")
  53.         settings = DefaultSettings
  54.         save_settings()
  55.     else:
  56.         if verbose: print("Settings.gd, load_settings || Settings file found!")
  57.         var source = FileAccess.open(SettingsPath, FileAccess.READ).get_as_text()
  58.         settings = JSON.new().parse_string(source)
  59.         if verbose: print("Settings.gd, load_settings || settings: \n" + str(settings))
  60.  
  61.  
  62. func save_settings(verbose : bool = false):
  63.     var new_file = FileAccess.open(SettingsPath, FileAccess.WRITE)
  64.     new_file.store_string(JSON.new().stringify(settings))
  65.     new_file.close()
  66.     if verbose: print("Settings.gd, save_settings || Settings file saved.")
  67.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement