Dictionary: choiceData

To explore a simple example of using a C# Dictionary to store user choices, imagine that a player can choose one of 2 or 3 different options that correspond to unlocking or locking some options within another scene or game situation. Since we're creating a simple prototype, we can provide the options as buttons within a panel that opens after some dialog has completed that gives back-story of the choice and options.

GameData.cs: Create a C# Dictionary: choiceData

When creating a C# Dictionary is necessary to determine the dataType of the < key, value > elements that it will hold. A very flexible option is to use string for the both key and the value, so that's what we'll use in this example. Keep in mind, dictionaries are very flexible, the dataType of the value can be an entire dictionary. Imagine a string-type key of "food" where the associated value being an entire dictionary of different food types and quantities, where the whole dictionary is stored in a scriptableObject, so it could be exported/ imported into any project. Dictionaries are very useful data-structures for storing game-type information.

Define, Initialize C# Dictionary< string, string > choiceData

First, we'll add a Dictionary< string, string> to GameData, then we add methods to add or update a key-value pair and a method to access the value that is stored with a key.

See Updated GameData version 3 Code

GameData Code Snippets to declare, add, access dictionary data.

Declare and initialize C# Dictionary< string, string >

//Declare private Dictionary ref-variable: choiceData
private Dictionary<string,string> choiceData = new Dictionary< string,string>();

Define GameData public method SaveChoice for adding and modifying Dictionary elements.

public void SaveChoice( string choiceKey, string choiceValue)
    {

        if (choiceData.ContainsKey(choiceKey))
        {
            choiceData[choiceKey] = choiceValue; //change stored value
            Debug.Log("Choice Changed" + choiceKey + " : " + choiceValue);
        }
        else
        {
            choiceData.Add(choiceKey, choiceValue); //adds key,value pair
            Debug.Log("Choice Data Created" + choiceKey + " : " + choiceValue);
        }
    } //end method

Define GameData public method GetChoice to allow access, return of a value for a given key

//returns the string value associated with the choiceKey
public string GetChoice( string choiceKey)
    {
        string choiceValue = "None";
        choiceData.TryGetValue(choiceKey, out choiceValue);
        Debug.Log("Choice Data Accessed" + choiceKey + " : " + choiceValue);
        return choiceValue;
    } //end method

Optional can be added to GameData if desired: Define GameData public method RemoveChoice to remove a matching key, value pair from the dictionary.


  //removes item from dictionary if key exists
    public void RemoveChoice( string choiceKey)
    {
        if(choiceData.ContainsKey(choiceKey))
        {
            choiceData.Remove(choiceKey);
        }
    }

Last updated