Advertisement
Neverknow247

save_and_load.gd

Oct 5th, 2023 (edited)
78
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 2.31 KB | Source Code | 0 0
  1. extends Node
  2.  
  3. var stats = Stats
  4. var utils = Utils
  5.  
  6. var dev_mode = stats.dev_mode
  7.  
  8. var SAVE_DATA_PATH
  9. var SAVE_SETTINGS_PATH
  10.  
  11. var default_save_data = stats.new_save_data
  12.  
  13. func _ready():
  14.     var logged_settings_path
  15.     if dev_mode == true:
  16.         logged_settings_path = "res://save_data/settings.cfg"
  17.     else:
  18.         logged_settings_path = "user://settings.cfg"
  19.     SAVE_SETTINGS_PATH = logged_settings_path
  20.  
  21. func change_save_location():
  22.     var logged_data_path
  23.     if dev_mode == true:
  24.         logged_data_path = "res://save_data/%s_save_data.dat" % [stats.save_slot]
  25.     else:
  26.         logged_data_path = "user://%s_save_data.dat" % [stats.save_slot]
  27.     SAVE_DATA_PATH = logged_data_path
  28.  
  29. func save_all():
  30.     update_save_data()
  31.     update_settings()
  32.  
  33. func save_data_to_file(save_data):
  34.     var file = FileAccess.open(SAVE_DATA_PATH, FileAccess.WRITE)
  35.     file.store_var(save_data)
  36.     file.close()
  37.  
  38. func load_data_from_file():
  39.     if not FileAccess.file_exists(SAVE_DATA_PATH):
  40.         return default_save_data
  41.     var file = FileAccess.open(SAVE_DATA_PATH, FileAccess.READ)
  42.     var save_data = file.get_var()
  43.     if save_data.version < default_save_data.version:
  44.         save_data = check_old_data(save_data)
  45.     elif save_data.version > default_save_data.version:
  46.         get_tree().change_scene_to_file("res://menus/new_version_screen.tscn")
  47.     return save_data
  48.  
  49. func update_save_data():
  50.     var save_data = load_data_from_file()
  51.     for stat in stats.save_data:
  52.         save_data[stat] = stats.save_data[stat]
  53.     SaveAndLoad.save_data_to_file(save_data)
  54.     load_data()
  55.  
  56. func load_data():
  57.     var save_data = load_data_from_file()
  58.     for stat in save_data:
  59.         stats.save_data[stat] = save_data[stat]
  60.  
  61. func check_old_data(save_data):
  62.     var version = default_save_data.version
  63.     for data in default_save_data:
  64.         if !data in save_data:
  65.             save_data[data] = default_save_data[data]
  66.     save_data.version = version
  67.     return save_data
  68.  
  69.  
  70. #SAVE AND LOAD SETTINGS
  71.  
  72. func update_settings():
  73.     var settings = ConfigFile.new()
  74.     settings.set_value("volume_settings","setting",utils.volume_settings)
  75.     settings.save(SAVE_SETTINGS_PATH)
  76.     load_settings()
  77.  
  78. func load_settings():
  79.     var settings = ConfigFile.new()
  80.     var err = settings.load(SAVE_SETTINGS_PATH)
  81.     if err != OK:
  82.         return
  83.     for setting in settings.get_sections():
  84.         var single_setting = settings.get_value(setting, "setting")
  85.         utils[setting] = single_setting
  86.  
  87.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement