Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Managing Global Decision Variables

Discussion in 'Getting Started' started by SamMurphy727, Mar 7, 2023.

  1. SamMurphy727

    SamMurphy727

    Joined:
    May 2, 2022
    Posts:
    2
    Hey!

    Working on a rough idea for a game where you can walk around and chat with people, and your decisions are stored in some global variable. You will then have different dialogue options / different scenes will load as a result of your decisions.

    I've been handling the movement and dialogue with TopDown Engine and DialogueSystem respectivly but have been running into issues trying to store player decisions and would love some rough direction.

    At the moment "Actions" are objects that can be interacted with, and once you interact with them it enables a UI element. The UI element question and choice text is populated by a scriptable object on the gameobject being interacted with - all working grand so far. The issue is updating the OnClick element of the buttons to store the values correctly. I've tried passing in a String with the "VariableInQuestion" as part of the scriptable object but can't figure out how to update my "Big Book of Variables" global controller at the string in question. Would love any insight into how to handle global variable storage, pretty new to Unity outside of a couple of GameDevTV tutorials!

    Rough overview of how it plays below, at the moment the buttons aren't firing anything though:
     
  2. SamMurphy727

    SamMurphy727

    Joined:
    May 2, 2022
    Posts:
    2
    Figured it out! Reflections etc
    Code below if it's helpful to anyone else. Put this in the script with your big list of variables and it works.
    }
    Code (CSharp):
    1. public void MakeVariableTrue(string variableName) {
    2.         var field = GetType().GetField(variableName);
    3.         if (field != null) {
    4.             field.SetValue(this, true);
    5.         } else {
    6.             Debug.LogError($"Variable {variableName} not found in {GetType().Name}");
    7.         }
    8.  
    9.     }
     
  3. AnaWilliam850

    AnaWilliam850

    Joined:
    Dec 23, 2022
    Posts:
    36
    It sounds like you are on the right track with using a "Big Book of Variables" global controller to store player decisions. Here are some steps you can take to implement this in your game:

    1. Create a script to store global variables - Create a new C# script in your Unity project and name it something like "GlobalVariables". In this script, you can define public static variables to store the player's decisions, such as "playerName" or "playerGender". These variables will be accessible from any other script in your game.

    2. Set global variables in your dialogue system - When the player makes a decision in the dialogue system, you can set the corresponding global variable in your "GlobalVariables" script. For example, if the player chooses a gender in the dialogue, you can set the "playerGender" variable to the selected value.

    3. Access global variables in other scripts - In any other script that needs to reference the player's decisions, you can access the corresponding global variable in the "GlobalVariables" script. For example, if you have a script that changes dialogue options based on the player's gender, you can check the value of "playerGender" in the "GlobalVariables" script and update the dialogue accordingly.
    To update the "Big Book of Variables" global controller at the string in question, you can add a public static method to your "GlobalVariables" script that takes a string parameter for the variable name and an object parameter for the new value. Inside this method, you can use a switch statement to set the correct global variable based on the variable name parameter.

    For example, your method might look like this:

    csharp
    public static void SetGlobalVariable(string variableName, object value)
    {
    switch (variableName)
    {
    case "playerName":
    playerName = (string)value;
    break;
    case "playerGender":
    playerGender = (string)value;
    break;
    // Add more cases for each global variable you want to store
    }
    }

    Then, in your dialogue system, you can call this method when the player makes a decision:

    python
    GlobalVariables.SetGlobalVariable("playerGender", "male");

    This would set the "playerGender" global variable to "male". You can repeat this process for each variable you want to store.

    I hope this helps you implement global variable storage in your game.