admin管理员组

文章数量:1026989

I am currently making an options menu for my game and obviously one of the settings is to make a fullscreen. I want to have it so that when you click for it to be fullscreen, and it toggles on, and you leave the scene and come back to it, it remains to show that it is on. However, currently, it restores back to being off, yet it keeps the state of being fullscreen. To change back to windowed if you come back to the options menu, you have to click it twice, one to "set it back" to fullscreen, and then the second which recognises it was fullscreen and now needs to be windowed. Here is my code: func _on_fullscreen_toggled(_toggled_on: bool) -> void: #Sets the state of fullscreen whether it is windowed or not if _toggled_on: get_window().set_mode(Window.MODE_FULLSCREEN) else: get_window().set_mode(Window.MODE_WINDOWED)

I am currently making an options menu for my game and obviously one of the settings is to make a fullscreen. I want to have it so that when you click for it to be fullscreen, and it toggles on, and you leave the scene and come back to it, it remains to show that it is on. However, currently, it restores back to being off, yet it keeps the state of being fullscreen. To change back to windowed if you come back to the options menu, you have to click it twice, one to "set it back" to fullscreen, and then the second which recognises it was fullscreen and now needs to be windowed. Here is my code: func _on_fullscreen_toggled(_toggled_on: bool) -> void: #Sets the state of fullscreen whether it is windowed or not if _toggled_on: get_window().set_mode(Window.MODE_FULLSCREEN) else: get_window().set_mode(Window.MODE_WINDOWED)

Share Improve this question asked Nov 16, 2024 at 1:34 DaGoldenGam3rDaGoldenGam3r 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You have to persist your settings. One way doing this is to save them in a file.

Create a new script file calling it settings.gd

The code would look like something like this:

extends Node

var config = ConfigFile.new()

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    load_settings()

func load_settings() -> void:
    var err = config.load("user://settings.cfg")
    # If the file didn't load, ignore it.
    if err != OK:
        return

func save_settings() -> void:
    config.save("user://settings.cfg")

func set_setting(name, value) -> void:
    config.set_value("settings", name, value)

func get_setting(name):
    return config.get_value("settings", name)

This is a simple code, which tries to load a file from a path. It also gets two functions to get and set settings. This class now belongs into your global scripts (located in project settings/Globals). I called it Settings:

This will give you the global Variable Settings, which you can use in all your other scripts.

Now in your Settings scene you want to retrieve all necessary informations from this file. So we get the informations in on_ready:

func _ready() -> void:
    $FullscreenToggle.button_pressed = false
    var fs_setting = Settings.get_setting("fullscreen")
    if (fs_setting != null and fs_setting is bool):
        $FullscreenToggle.button_pressed = fs_setting

FullscreenToggle is a Checkbutton in my sample project. I default to false, since here, but I guess you should set it according to your start options. Then I try to read the setting "fullscreen". This will be null on the first try, because the file has not been written yet.

If the user toggles the button you do your code and set the "fullscreen" option in the file:

func _on_check_button_toggled(toggled_on: bool) -> void:
    Settings.set_setting("fullscreen", toggled_on)
    if toggled_on:
        get_window().set_mode(Window.MODE_FULLSCREEN) 
    else: 
        get_window().set_mode(Window.MODE_WINDOWED)
    Settings.save_settings() #You could add aa confirm button and only do this after he set all settings

This will save it into the file and the next time you open the settings scene it will read the setting from the file and set the button accordingly

The reason I doing it in a global script is, that you now are able to load the settings when the player opens your game. So you can also start the game fullscreen, if the player set it that way.

I am currently making an options menu for my game and obviously one of the settings is to make a fullscreen. I want to have it so that when you click for it to be fullscreen, and it toggles on, and you leave the scene and come back to it, it remains to show that it is on. However, currently, it restores back to being off, yet it keeps the state of being fullscreen. To change back to windowed if you come back to the options menu, you have to click it twice, one to "set it back" to fullscreen, and then the second which recognises it was fullscreen and now needs to be windowed. Here is my code: func _on_fullscreen_toggled(_toggled_on: bool) -> void: #Sets the state of fullscreen whether it is windowed or not if _toggled_on: get_window().set_mode(Window.MODE_FULLSCREEN) else: get_window().set_mode(Window.MODE_WINDOWED)

I am currently making an options menu for my game and obviously one of the settings is to make a fullscreen. I want to have it so that when you click for it to be fullscreen, and it toggles on, and you leave the scene and come back to it, it remains to show that it is on. However, currently, it restores back to being off, yet it keeps the state of being fullscreen. To change back to windowed if you come back to the options menu, you have to click it twice, one to "set it back" to fullscreen, and then the second which recognises it was fullscreen and now needs to be windowed. Here is my code: func _on_fullscreen_toggled(_toggled_on: bool) -> void: #Sets the state of fullscreen whether it is windowed or not if _toggled_on: get_window().set_mode(Window.MODE_FULLSCREEN) else: get_window().set_mode(Window.MODE_WINDOWED)

Share Improve this question asked Nov 16, 2024 at 1:34 DaGoldenGam3rDaGoldenGam3r 111 bronze badge
Add a comment  | 

1 Answer 1

Reset to default 0

You have to persist your settings. One way doing this is to save them in a file.

Create a new script file calling it settings.gd

The code would look like something like this:

extends Node

var config = ConfigFile.new()

# Called when the node enters the scene tree for the first time.
func _ready() -> void:
    load_settings()

func load_settings() -> void:
    var err = config.load("user://settings.cfg")
    # If the file didn't load, ignore it.
    if err != OK:
        return

func save_settings() -> void:
    config.save("user://settings.cfg")

func set_setting(name, value) -> void:
    config.set_value("settings", name, value)

func get_setting(name):
    return config.get_value("settings", name)

This is a simple code, which tries to load a file from a path. It also gets two functions to get and set settings. This class now belongs into your global scripts (located in project settings/Globals). I called it Settings:

This will give you the global Variable Settings, which you can use in all your other scripts.

Now in your Settings scene you want to retrieve all necessary informations from this file. So we get the informations in on_ready:

func _ready() -> void:
    $FullscreenToggle.button_pressed = false
    var fs_setting = Settings.get_setting("fullscreen")
    if (fs_setting != null and fs_setting is bool):
        $FullscreenToggle.button_pressed = fs_setting

FullscreenToggle is a Checkbutton in my sample project. I default to false, since here, but I guess you should set it according to your start options. Then I try to read the setting "fullscreen". This will be null on the first try, because the file has not been written yet.

If the user toggles the button you do your code and set the "fullscreen" option in the file:

func _on_check_button_toggled(toggled_on: bool) -> void:
    Settings.set_setting("fullscreen", toggled_on)
    if toggled_on:
        get_window().set_mode(Window.MODE_FULLSCREEN) 
    else: 
        get_window().set_mode(Window.MODE_WINDOWED)
    Settings.save_settings() #You could add aa confirm button and only do this after he set all settings

This will save it into the file and the next time you open the settings scene it will read the setting from the file and set the button accordingly

The reason I doing it in a global script is, that you now are able to load the settings when the player opens your game. So you can also start the game fullscreen, if the player set it that way.

本文标签: user interfaceHow to Save Fullscreen State Between Scene Loads in Godot 43Stack Overflow