Game Development Reference
In-Depth Information
}
}
The preceding code is fairly similar but it ensures simple and effective use of any diction-
ary class, checking the following:
• When you request a value from the dictionary, it checks whether it exists first and
then returns it
• If the value doesn't exist in the dictionary yet, it returns a default value
• When you add a new value to the dictionary, it checks whether it already exists,
and if it does, then it updates the existing value
• If the value does not exist when you try to add it, it just adds it to the dictionary
Tip
Dictionaries are powerful when used correctly: you can find values by index (in this case
a string) or you can find them by ID (like in arrays). You can even loop over dictionaries
with for or foreach loops.
However, depending on how you use them, they may not perform well and can also gen-
erate garbage, so use them carefully.
For more details, see the C# article at http://blogs.msdn.com/b/shawnhar/archive/2007/07/
02/twin-paths-to-garbage-collector-nirvana.aspx . The article is based on XNA but rings
true for any C# platform.
There are also considerations when you need to serialize the values from a dictionary
since they are handled differently on some platforms, and in some cases not even suppor-
ted for serialization.
With the GameState class in place, we just need to update the MapMovement script
for the map to load the last position if one exists, and save the last position when exiting
the scene (and in any other scene that will need the logic).
So, update the MapMovement script's Awake method with the following code:
void Awake()
{
this.collider2D.enabled = false;
var lastPosition =
Search WWH ::




Custom Search