Godot Version
4.2.2
Question
I make a settings menu in my game. And I need to make it so that in the drop-down menu I can choose the type of control between the two options. Arrow control, and control on “WASD”. I started doing this using “match”, which reads the string value of the variable at the beginning of the code. And I need the settings script to be able to interact with the variable in the control script and change it. Please help!
You might want to consider using a global_script for this, then you can access it from everywhere.
When settings are selected you save it to the Global-scene called e.g. “Settings”
Settings.controls = # your controls here
Then you can access it from your other scripts:
my_controls = Settings.controls
There are different ways to do this. The easiest way would be to create a script with a class name that holds static functions and/or variables. Making an autoload class would be great way to organize that.
class_name Database extends Node
static var Name : String = "Adam"
class_name Actor extends Node
func ChangeName() -> void:
Database.Name = "Michael"
You can also use get/set to basically share the same variables. You can also add additional functionality to make sure min/max are set.
class_name Settings extends Node
static var Volume : int = 100
class_name VolumeBar extends Node
var Current_Volume : int :
get : return Database.Volume
set(value) : Database.Volume = clamp(value, 0, 100)
Or you can even use arrays and dictionaries to manage data. Which makes saving the game easier, because data is stored in a convenient place.
class_name Database extends Node
static var Chest_List : Dictionary = { "Lv1_Chest1": false, "Lv1_Chest2": true }
class_name Chest extends Node
@export var Chest_ID : String
var Is_Chest_Open : bool :
get : return Database.Chest_List[Chest_ID]
set(value) : Database.Chest_List[Chest_ID] = value
func Pickup() -> void:
if Is_Chest_Open == false:
Is_Chest_Open = true
# Perform Animation and Add items
# Change State = Opened Chest