Game Development Reference
In-Depth Information
36 //Output current score and exit function
37 return Score;
38 }
39 }
The following is the breakdown of the code present for code sample 1-8:
Line 08 : A private, integer class variable Score is declared to keep track
of a sample score value. This variable will be used later in the function
UpdateScore .
Lines 11, 23, and 28 : The class MyScriptFile has three functions (sometimes
called methods or member functions). These are Start , Update , and
UpdateScore . Start and Update are special functions that Unity provides,
as we'll see shortly. UpdateScore is a custom function for MyScriptFile .
Line 28 : The UpdateScore function represents a complete block of code
between lines 29 and 38. This specific function should be invoked every time
the game score must change. When called, the code block (lines 29-38) will
be executed sequentially. In this way, functions offer us code recyclability.
Lines 14-19 : The UpdateScore function is called several times during the
Start function. For each call, the execution of the Start function pauses
until the UpdateScore function completes. At this point, the execution
resumes in the next line.
Line 28 : UpdateScore accepts two parameters or arguments. These are
an integer AmountToAdd and a Boolean PrintToConsole . Arguments act
like inputs we can plug in to the function to affect how they operate. The
AmountToAdd variable expresses how much should be added to the current
Score variable, and PrintToConsole determines whether the Score variable
should be shown in the Console window when the function is executed.
There is theoretically no limit to the number of arguments a function can
have, and a function can also have no arguments at all, such as the Start
and Update functions.
Lines 31-34 : Here, the score is actually updated and printed to Console ,
if required. Notice that the PrintToConsole argument has a default value
of true already assigned to the function declaration in line 28. This makes
the argument optional whenever the function is called. Lines 14, 15, and 16
explicitly override the default value by passing a value of false . Line 19,
in contrast, omits a second value and thereby accepts the default of true .
 
Search WWH ::




Custom Search