Game Development Reference
In-Depth Information
Lines 30 and 31 : Here, two case statements follow one another. The code
block in lines 33 and 34 will be executed if, and only if, ActiveState is
either EnemyState.Flee or EnemyState.Hide .
Line 38 : The default statement is optional for a switch statement. When
included, it will be entered if no other case statements are true . In this
case, it would apply if ActiveState is EnemyState.Chase .
Lines 27, 36, and 44 : The break statement should occur at the end of a case
statement. When it is reached, it will exit the complete switch statement to
which it belongs, resuming program execution in the line after the switch
statement, in this case, line 45.
More information on the switch statement and its usage in
C# can be found at http://msdn.microsoft.com/en-GB/
library/06tc147t.aspx .
Arrays
Lists and sequences are everywhere in games. For this reason, you'll frequently need
to keep track of lists of data of the same type: all enemies in the level, all weapons
that have been collected, all power ups that could be collected, all spells and items
in the inventory, and so on. One type of list is the array. Each item in the array is,
essentially, a unit of information that has the potential to change during gameplay,
and so a variable is suitable to store each item. However, it's useful to collect together
all the related variables (all enemies, all weapons, and so on) into a single, linear, and
traversable list structure. This is what an array achieves. In C#, there are two kinds of
arrays: static and dynamic. Static arrays might hold a fixed and maximum number of
possible entries in memory, decided in advance, and this capacity remains unchanged
throughout program execution, even if you only need to store fewer items than the
capacity. This means some slots or entries could be wasted. Dynamic arrays might
grow and shrink in capacity, on demand, to accommodate exactly the number of
items required. Static arrays typically perform better and faster, but dynamic arrays
feel cleaner and avoid memory wastage. This chapter considers only static arrays,
and dynamic arrays are considered later, as shown in the following code sample 1-4:
01 using UnityEngine;
02 using System.Collections;
03
04 public class MyScriptFile : MonoBehaviour
05 {
06 //Array of game objects in the scene
07 public GameObject[] MyObjects;
08
 
Search WWH ::




Custom Search