Home > database >  How to find a boolean in a list with a string in Unity?
How to find a boolean in a list with a string in Unity?

Time:06-29

I am creating a debug menu for my game with all the boolean in my GameManager exposed in a in-game UI for my non-programmer teammates.

I am trying to find a bool in a list with a string and I’m not sure how to do it.

This is my pseudocode:

[SerializeField] string myBoolName;

private void LookForBool()    
{
    foreach (var bolean in debugMenu.gameManagerBoolList)
    {
        if (debugMenu.gameManagerBoolList.Contains(bolean))
        {
            myBoolName = bolean;
        }
    }
  
}

public void SwitchBoolWithToggle() //Will be called in a UI toggle
{
    switch (toggle.isOn)
    {
        case true:
            myBoolName = true;
            break;
        case false:
            myBoolName = false;
            break;
    }
}

Is there any way to do this? I’m still consider myself a beginner.

Thank you!

CodePudding user response:

It is a bit unclear what exactly you are trying to do here.

If you iterate over

foreach (var bolean in debugMenu.gameManagerBoolList)

then it should be quite obvious that each and every of these bolean elements of debugMenu.gameManagerBoolList are of course contained by that debugMenu.gameManagerBoolList.


Why not rather use a dictionary Dictionary<string, bool> or maybe even use an enum as key so you could even simply select it via the Inspector ad avoid any typos.

The default drawer of Unity for enums in the Inspector is a simple dropdown field ;)

I would do something like this

public enum AvailableBools
{
    SomeKey,
    SomeOtherKey,
    ...
}

and then in your manager class

private readonly Dictionary<AvailableBools, bool> bools = new Dictionary<AvailableBools, bool>();
public IDictionary<AvailableBools, bool> Bools => bools;

private void Awake()
{
    foreach(var key in (AvailableBools[])Enum.GetValues(typeof(AvailableBools)))
    {
        // or true depending on your desired default state
        bools[key] = false;
    }
}

There are fancy assets like e.g. SerializableDictionary which you could use for simply initializing the default values via the Inspector if they have different initial values

[Serializable]
public class AvailablBoolsDictionary : SerializableDictionary<AvailableBools, bool>{}

[SerializeField] private AvailablBoolsDictionary bools;
public IDictionary<AvailableBools, bool> Bools => bools;

and then in your class rather simply do e.g.

[SerializeField] AvailableBools myBool;


public void OnValuChanged(bool isOn)
{
    debugMenu.Bools[myBool] = isOn;
}  

CodePudding user response:

I recommend the method here that you do not need any list at all, just define booleans with their own names.

public class Test : MonoBehaviour
{
    public bool loopAnimation;
    public bool attackTwice;
    public bool gameIsOver;

    public string myBoolName = "LoopAnimation"; // find boolean

///...
}

Next, to catch boolean by name, use the following commands that monitor field information.

private void OnValidate()
{
    foreach (var fieldInfo in typeof(Test).GetFields())
    {
        if (fieldInfo.FieldType == typeof(bool) && fieldInfo.Name == myBoolName)
        {
            Debug.Log(fieldInfo.Name ": " fieldInfo.GetValue(this)); // loopAnimation: true
        }
    }
}

The following command will also help to toggle booleans.

private void ToggleByName(string boolName)
{
    foreach (var fieldInfo in typeof(Test).GetFields())
    {
        if (fieldInfo.FieldType == typeof(bool) && fieldInfo.Name == boolName)
        {
            fieldInfo.SetValue(this, ! (bool) fieldInfo.GetValue(this));
        }
    }
}
  • Related