Converting a string to a boolean referenced in an other script

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Dostoi

I am trying to get the value of a bool loaded in a singleton pattern. This works:

	_gameVariables = GetNode<gameVariables>("/root/GameVariables");
	GD.Print(_gameVariables.boolExample);

This returns the correct value of the bool named “boolExample”.

However, I also need to be able to change the reference of the bool that is checked from one instance of the script to an other. So here’s what I tried that doesn’t work:

	_gameVariables = GetNode<gameVariables>("/root/GameVariables");
    variableToCheck = "boolExample";
    bool value = Convert.ToBoolean("_gameVariables." + variableToCheck);

    GD.Print(value);

Although “GD.Print” never prints any value for it returns the following error “Boolean System.Boolean.Parse(System.ReadOnlySpan`1[System.Char] ): System.FormatException: String was not recognized as a valid Boolean.”

What am I doing wrong ?

Thank you for your time.

Rather than trying to access values by constructing variable names from string references, you’re probably better off storing all of the values you’re interested in in a single, global (singleton) Dictionary where the key name is your “variable” and the key value is your value. That way, you just need a reference to the single Dictionary, and you can easily access any key (and therefore its value) by its string name.

jgodfrey | 2022-03-23 18:42

That’s actually a more elegant solution. I’m not used to the “etiquette” of that forum so I’ll just post what I did here, based on your advice (which would be the answer of that question).

For future references:

I’ve created a dictionary loaded in “autoload”:

gamevariables = new Godot.Collections.Dictionary
{
    { "ExampleKeyA", "true"},
    { "ExampleKeyB", "false"},
    { "ExampleKeyC", "true"},
};

Then, in a script that is meant to be instanciated, I’ve wrote this (working) code:

		[Export]
		public string variableToCheck;

		if (_gameVariables.gamevariables[variableToCheck].Equals("true"))
		{
			///Do things
		}

Thank you jgodfrey.

Dostoi | 2022-03-23 19:56

Keep in mind, the values in your Dictionary can be a mix of types. So, rather than storing the boolean value as a string you can actually store it as a boolean - which makes testing it less verbose and (presumably) faster…

For example:

var dict = {"a": true, "b": 10, "c": "some string"}
if dict["a"]:
    print("TRUE")
else:
    print("FALSE")

jgodfrey | 2022-03-23 20:56

:bust_in_silhouette: Reply From: jgodfrey

Converting the initial comment above to an Answer for visibility on the main page:

Rather than trying to access values by constructing variable names from string references, you’re probably better off storing all of the values you’re interested in in a single, global (singleton) Dictionary where the key name is your “variable” and the key value is your value. That way, you just need a reference to the single Dictionary, and you can easily access any key (and therefore its value) by its string name.

(see conversation above for more details).