2023-01-29 10:41:06 -07:00
extends Node
const SETTINGS_FILE = " user://settings.vars " # user:// path varies depending on operating system. See https://docs.godotengine.org/en/latest/tutorials/io/data_paths.html#accessing-persistent-user-data-user
var settings_template = { # Default values if we don't already have a settings file. Assigned in save_data()
' mousesense ' : 0.01 ,
2023-03-17 00:20:54 -06:00
' playername ' : " player " ,
2023-04-15 15:04:16 -06:00
' volume ' : 0.5 ,
' tutorialCompleted ' : false
2023-01-29 10:41:06 -07:00
}
var settings_data = { } # This gets filled as soon as load_data() is called.
func _ready ( ) :
check_data ( )
load_data ( )
func check_data ( ) : # Makes sure the save file exists
var file = FileAccess . open ( SETTINGS_FILE , FileAccess . READ ) #If we write, it writes an empty file over our existing one
if not file : # If there's no file, let's create one with FileAccess.WRITE
2023-03-06 18:43:31 -07:00
if OS . has_environment ( " USERNAME " ) :
print ( " env has USERNAME var " )
2023-03-08 22:21:35 -07:00
settings_template [ " playername " ] = OS . get_environment ( " USERNAME " )
2023-03-06 18:43:31 -07:00
elif OS . has_environment ( " USER " ) :
print ( " env has USER var " )
settings_template [ " playername " ] = OS . get_environment ( " USER " )
else :
print ( " env has no known user var " )
2023-01-29 10:41:06 -07:00
print ( " file does not exist, let ' s create one! " )
var file2 = FileAccess . open ( SETTINGS_FILE , FileAccess . WRITE )
file2 . store_line ( JSON . stringify ( settings_template ) ) # Writes settings_template to our file SETTINGS_FILE
2023-04-15 15:04:16 -06:00
func save_data ( ) :
2023-01-29 10:41:06 -07:00
var file = FileAccess . open ( SETTINGS_FILE , FileAccess . WRITE )
2023-04-15 15:04:16 -06:00
settings_data [ ' mousesense ' ] = Global . mouseSensitivity
settings_data [ ' playername ' ] = Global . playerName
settings_data [ ' volume ' ] = Global . volumeModifer
settings_data [ ' tutorialCompleted ' ] = Global . tutorialCompleted
2023-02-16 13:27:34 -07:00
file . store_line ( JSON . stringify ( settings_data ) )
2023-01-29 10:41:06 -07:00
func load_data ( ) :
var file = FileAccess . open ( SETTINGS_FILE , FileAccess . READ )
settings_data = JSON . parse_string ( file . get_line ( ) )
print ( settings_data )
2023-02-16 13:27:34 -07:00
var mousesense = settings_data [ ' mousesense ' ]
var playername = settings_data [ ' playername ' ]
2023-03-17 00:20:54 -06:00
var volume = settings_data [ ' volume ' ]
2023-04-15 15:04:16 -06:00
var tutorialCompleted = settings_data [ ' tutorialCompleted ' ]
2023-02-16 13:27:34 -07:00
Global . mouseSensitivity = mousesense
Global . playerName = playername
2023-03-17 00:20:54 -06:00
Global . volumeModifer = volume
2023-04-15 15:04:16 -06:00
Global . tutorialCompleted = tutorialCompleted