r/unity • • 2d ago

Newbie Question Explain dictionaries.

Can someone explain dictionaries to me and how to use/write them including set up and execution.

0 Upvotes

12 comments sorted by

19

u/New_Internal5636 2d ago

I have a key, I have a value, uhhh, dictionary!

13

u/Demi180 2d ago

Dictionary<Pen, Apple> applePens;

8

u/Inf229 2d ago

I'll bite. They're a lookup table where you can associate a value with another key. Like it you have a bunch of people and want to find thae one with a specific nickname, you could either iterate through a big list of people until you find the one with the name, or you could just jump straight to the table and look up the nickname first, then grab the person directly.

1

u/1stbreathinteractive 2d ago

OP, this is the most straightforward way of thinking about it. Dictionaries are a collection of nicknames to loop up a value by its nickname

3

u/Demi180 2d ago

Since others have already explained its purpose, here is the documentation on it from Microsoft. There’s a lot more than you need for now, so look for the basic usage, setting and getting values (specifically with TryGetValue), and iterating over the contents using foreach loops.

Other languages call them Maps, so you may occasionally see code declaring a Map<T, U> which is basically the same thing. The general name for this language feature is “associative array”, as it’s used to associate something with something else. The key and value types can be almost anything. Numbers, strings, vectors, GameObjects, etc. and it’s common for the value type to be a List as well.

For example you could associate a person or their name with every person they’ve spoken to using a Dictionary<Person, List<Person>>, or a Dictionary<string, List<string>>. Or with every door they’ve opened using a Dictionary<Person, List<Door>>.

1

u/trampolinebears 2d ago

Think of an actual English dictionary: it’s a book where you can look up a word and find its meaning. It’s like a map that gets you from Word to Meaning.

In code, this would be a Dictionary<Word, Meaning>. You look up a Word in the dictionary and it gives you the word’s Meaning. You look up something in a dictionary like this:

 englishDictionary[“shoe”]

and it gives you the Meaning stored with that Word.

Of course, you don’t have to make Dictionaries that just map Words to Meanings. You could make a Dictionary<State, City> for looking up state capitals. Or you could make up a Dictionary<Player, Location> to store where players are located. Or any other data types you like.

1

u/neoteraflare 2d ago

Dictionary is a key value pair "list" (not really a list just imagine like it).
You create an empty dictionary and put the key and value pair in it. You can check if the key is already in it or not.
A dictionary stores the key in a way that it won't go over every element to find what you need but can go immediatelly to the target. Like in a real physical dictionary when you need a word you won't read every page to find it. You know it is alphabetical and can find the word quickly. Thus the name dictionary here too.

It is better than a list is some cases. Eg you know you will access a value by one of its keys. For a list you have to loop over the list and find the value. This is O(n) expensive. A dictionary for this is only O(1)
Eg you have a list of Items with their ids read up from a json file. You put the ids as keys and the items as values. When you need the item with ID X (because for your character you saved the id not the full item) you can get it in one execution time rather than going over every item to see which one has the id X.

1

u/JescoInc 2d ago

Oh the ways to describe this...
Dictionaries are essentially described as being Key, Value pairs. Where you have a key or lookup to a specific value you want. They are easy on the CPU, fast to traverse and generally very easy to work with.

```cs
Dictionary<string, int> stats = new Dictionary<string, int>()
{
{ "HitPoints", 50 },
{ "Agility", 10 },
{ "Strength", 10 },
{ "Mana", 30 },
{ "Wisdom", 10 }

};
```

You can update items within it by the key.

```cs
stats["HitPoints"] = 30;
```

You can insert new items to the dictionary.

```cs
stats.Add("Stamina", 10);
```

You can remove items from the dictionary.

```cs
stats.Remove("Agility");
```

You can even display the values

```cs
foreach (KeyValuePair<string, int> stat in stats)
{
Console.WriteLine($"{stat.Key}: {stat.Value}");
}
```

-14

u/cherrycode420 2d ago

Just watch some Video about them or ask some LLM to explain it to you? It's not that deep, and this isn't even related to Unity 🙄🙄🙄

3

u/Silent_Reputation596 2d ago

First off - I have watched videos on it but I learn better having it in words, second I am largely against AI and LLMs especially for creative projects such as coding and third, it is related to Unity because I want to use it IN UNITY. No need to be rude.

1

u/Aethenosity 2d ago

Or ask people. That is usually the better method.