Advertisement
MarkJD68

Panel_Node Script

Jun 10th, 2023
1,577
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
GDScript 1.63 KB | Source Code | 0 0
  1. extends Panel
  2.  
  3.  
  4. #SAVE STUFF
  5. const SAVE_DIR = "user://saves/" #This is useful for HTML5 as well.
  6. #.json
  7. var SAVE_PATH = SAVE_DIR + "save_options.json"
  8.  
  9. # Called when the node enters the scene tree for the first time.
  10. func _ready():
  11.     load_data()
  12.    
  13.     pass # Replace with function body.
  14.  
  15. func _save():
  16.     Globals._ensure_directory_exists(SAVE_DIR)
  17.    
  18.     #1. Initialize a Data Dictionary
  19.     var save_dict = {}
  20.    
  21.     #Audio
  22.     save_dict = {
  23.             "MasterVolume": $AudioConfigurationMenu/Panel/MasterVolume/HSlider.value,
  24.             "SoundVolume": $AudioConfigurationMenu/Panel/SoundVolume/HSlider.value,
  25.             "MusicVolume": $AudioConfigurationMenu/Panel/MusicVolume/HSlider.value,
  26.         }
  27.    
  28.     #2. Create a file
  29.     var save_file = File.new()
  30.     save_file.open(SAVE_PATH,File.WRITE)
  31.  
  32.     #3. Serialize the data dictionary to JSON
  33.     var save_string = JSON.print(save_dict, "\t")
  34.     save_file.store_line(save_string)
  35.    
  36.     #4. Write the JSON to the file and save to disk
  37.     save_file.close()
  38.  
  39.     return save_dict
  40.  
  41.  
  42. func load_data():
  43.     # Try to load a file
  44.     var save_file = File.new()
  45.     if not save_file.file_exists(SAVE_PATH):
  46.         return
  47.    
  48.     # Parse the file data if it exists
  49.     save_file.open(SAVE_PATH,File.READ)
  50.    
  51.     var data = parse_json(save_file.get_as_text())
  52.    
  53.     #print(data["MasterVolume"])
  54.     $AudioConfigurationMenu/Panel/MasterVolume/HSlider.value = data["MasterVolume"]
  55.     $AudioConfigurationMenu/Panel/SoundVolume/HSlider.value = data["SoundVolume"]
  56.     $AudioConfigurationMenu/Panel/MusicVolume/HSlider.value = data["MusicVolume"]
  57.    
  58.     return data
  59.  
  60.  
  61. func _on_CheckBox_toggled(button_pressed):
  62.     OS.window_fullscreen = button_pressed
  63.     pass # Replace with function body.
  64.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement