0 votes

This is the AutoLoad Script

extends Node

const SAVE_PATH = "res://settings.cfg"
var configFile = ConfigFile.new()

var edited

var fullScreen = false
var screenResX = 1600
var screenResY = 900
var fov = 90
var mouseSens = 0.3
var vsync = true
var fS
var sRX
var sRY
var fV
var mS
var vC

func _ready():
    compare()
    if edited == false:
        default_settings()
    else:
        load_settings()

func default_settings():
    configFile.set_value("Video","Screen", fullScreen) 
    configFile.set_value("Video","ScreenSizeX", screenResX)
    configFile.set_value("Video","ScreenSizeY", screenResY)
    configFile.set_value("Game", "FOV", fov)
    configFile.set_value("Game", "MouseSensitivity", mouseSens)
    configFile.set_value("Video","Vsync",vsync) 
    # Save file
    configFile.save(SAVE_PATH)

func load_settings():
    fS = configFile.get_value("Video","Screen", fullScreen) 
    sRX = configFile.get_value("Video","ScreenSizeX", screenResX)
    sRY = configFile.get_value("Video","ScreenSizeY", screenResY)
    fV = configFile.get_value("Game", "FOV", fov)
    mS = configFile.get_value("Game", "MouseSensitivity", mouseSens)
    vC = configFile.get_value("Video","Vsync",vsync)
    OS.set_window_fullscreen(fS)
    OS.set_window_size(Vector2(sRX, sRY))
    OS.vsync_enabled = vC

func compare():
    if fS == fullScreen:
        edited = true
    elif sRX == screenResX:
        edited = true
    elif sRY == screenResY:
        edited = true
    elif fV == fov:
        edited = true
    elif mS == mouseSens:
        edited = true
    elif vC == vsync:
        edited = true

This is Settings script, it is attached to a Node2D

extends Node2D

const SAVE_PATH = "res://settings.cfg"
var configFile = ConfigFile.new()

var fullScreen = configFile.get_value("Video", "Screen")
var screenResX = configFile.get_value("Video", "ScreenSizeX")
var screenResY = configFile.get_value("Video", "ScreenSizeY")
var fov = configFile.get_value("Game", "FOV")
var mouseSensU = 3
var mouseSens = configFile.get_value("Game", "MouseSensitivity")
var vsync = configFile.get_value("Video","Vsync")

var fS
var sRX
var sRY
var fV
var mS
var vC

func _ready():
    load_settings()

func save_settings():
     # Add values to file
    configFile.set_value("Video","Screen", fullScreen) 
    configFile.set_value("Video","ScreenSizeX", screenResX)
    configFile.set_value("Video","ScreenSizeY", screenResY)
    configFile.set_value("Game", "FOV", fov)
    configFile.set_value("Game", "MouseSensitivity", mouseSens)
    configFile.set_value("Video","Vsync",vsync) 
    # Save file
    configFile.save(SAVE_PATH)
    print("SAVE WORKED")

func _on_FullScreen_toggled(button_pressed):
    fullScreen = !fullScreen
    print("FULLSCREEN WORKED")

func load_settings():
    fS = configFile.get_value("Video","Screen", fullScreen) 
    sRX = configFile.get_value("Video","ScreenSizeX", screenResX)
    sRY = configFile.get_value("Video","ScreenSizeY", screenResY)
    fV = configFile.get_value("Game", "FOV", fov)
    mS = configFile.get_value("Game", "MouseSensitivity", mouseSens)
    vC = configFile.get_value("Video","Vsync",vsync)
    OS.set_window_fullscreen(fS)
    OS.set_window_size(Vector2(sRX, sRY))
    OS.vsync_enabled = vC

func _on_SRX_text_entered(new_text):
    screenResX = new_text
    print("SCREENRES X WORKED")


func _on_SRY_text_entered(new_text):
    screenResY = new_text
    print("SCREENRESY WORKED")


func _on_FOVNUM_text_entered(new_text):
    fov = new_text
    print("FOV WORKED")


func _on_MouseSensNUM_text_entered(new_text):
    mouseSensU = new_text
    print("MOUSE SENSITIVITY WORKED")


func _on_VSYNC_toggled(button_pressed):
    vsync = !vsync
    print("VSYNC WORKED")


func _on_SAVE_pressed():
    save_settings()
    print("SAVE WORKED")


func _on_APPLY_pressed():
    load_settings()
    print("APPLY WORKED")

This was the error we got when we ran the settings script, why?

Invalid type in function 'set_window_fullscreen' in base '_OS'. Cannot convert argument 1 from Nil to bool.
in Engine by (144 points)

2 Answers

+1 vote

Hi,
That's surely because your fS variable is null instead that the boolean you intended to retrieve. I think you should load the config file in your Setting script, something like this:

extends Node2D

const SAVE_PATH = "res://settings.cfg"
var configFile = ConfigFile.new()
configFile.load(SAVE_PATH)
#REST OF THE CODE#

Cause actually, your configFile in Setting script is a new empty object, and when you try to get some value like with this: var fullScreen = configFile.get_value("Video", "Screen"), you just get null.

Thats what i think by reading this. If you share the project maybe i can help more. Please try and tell me!

by (3,501 points)

Hi,
Your problems seems to be that you don't have any default configuration saved. So when you try to read values from that files, it reads always null and fails before you could even save something. I think you should first check if file exists. If it does not, then create it with default values... if it does, then and only then attempt to read.

+1 vote

Apparently, the variable fS is not getting assigned the "video" configurations. It could be that, in the _ready() function, none of the previous code is being loaded. So the configFile object may not have any usable data, and thus will return the error you are receiving.

by (3,162 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.