Game Development Reference
In-Depth Information
//Remove this enemy from list
Enemies.RemoveAt(i);
}
}
The Dictionary class
The List class is, perhaps, one of the most useful classes in the Mono Framework
for in-memory data storage. However, let's not forget the Dictionary class (similar to
the std::map class in C++). This class is especially useful when you need more than
just a simple list of items. If you need to search for and get instant access to specific
elements based on a key value, then the Dictionary class is essential. For each item
in the list, you must save a corresponding key or ID that uniquely identifies the item
from all others. The Dictionary class then allows you to get instant access to this item,
based solely on its key. This makes the Dictionary class useful as a true dictionary for
word games, for example, if you need to look up the meaning or score-value of specific
words in a large dictionary or database of words. The word itself would be the key,
and the word definition would be the value.
Now, of course, you can replicate this kind of behavior using multiple List objects
instead of the Dictionary class. However, the Dictionary class is extremely fast
in terms of performance, almost lightning fast. You can store vast quantities of data
inside the dictionary at very little performance cost. This makes them highly valuable
for a quick data lookup from key values, as shown in the following code sample 6-3;
01 using UnityEngine;
02 using System.Collections;
03 using System.Collections.Generic;
04
05 public class Using_Dictionary : MonoBehaviour
06 {
07 //Database of words. <Word, Score> key-value pair
08 public Dictionary<string, int> WordDatabase = new
Dictionary<string, int>();
09
10 // Use this for initialization
11 void Start ()
12 {
13 //Create some words
14 string[] Words = new string[5];
15 Words[0]="hello";
16 Words[1]="today";
 
Search WWH ::




Custom Search