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 >
Define GameData public method SaveChoice for adding and modifying Dictionary elements.
Define GameData public method GetChoice to allow access, return of a value for a given key
Optional can be added to GameData if desired: Define GameData public method RemoveChoice to remove a matching key, value pair from the dictionary.
Last updated
Was this helpful?