Time for action – creating a timer in BeginState

I've modified BeginState quite a bit just for the timer. There are now three examples of ways to switch to PlayState. Plus, there's a timer countdown displayed in the upper right corner. I also put code that I had to write more than once into a new Switch() method.

  1. Edit BeginState as shown in the following screenshot.
  2. Save and play in Unity.
    Time for action – creating a timer in BeginState

What just happened?

I'm not going to walk you through the State switching code, I already did that previously.

An analysis of the code shown in the preceding screenshot is as follows: On the BeginState class

For lines 9, 10, and 11:

  • Three member variables are declared for use in the countdown timer
  • These will be explained later when they are used in code
  • The reason they are declared here as member variables is that they need to be used in more than one method
  • Remember the variable scope, variables are only visible within the code block they reside in

Line 16: futureTime = screenDuration + Time.realtimeSinceStartup;

  • The Time class is a Unity class
  • The Dot Syntax is used to access the value stored in the variable realtimeSinceStartup
  • This variable is constantly updated with the amount of seconds the game has been running
  • It stores a float type of value (decimal number)

    Note

    The Scripting Reference has a good description of realtimeSinceStartup, which is exactly the reason we are using it.

  • The current elapsed time is retrieved from realtimeSinceStartup and is added with the value in the screenDuration variable
  • The screenDuration variable was declared on line 11 with a value of 8 seconds, which is the length of time BeginState will be active before switching to PlayState
  • The result of the addition is assigned to the variable futureTime
  • Adding 8 seconds to the current elapsed time means I've set a time of 8 seconds into the future
  • The futureTime variable was declared as a float type on line 9

Line 17: Time.timeScale = 0;

  • Assigning 0 to the timeScale variable causes time in the game to standstill, thus pausing the game

    Note

    Both the lines 16 and 17 are executed in the BeginState constructor because it's only called once each time an instance object of BeginState is created. We want to pause the game at the instant BeginState is created. We also want to set the timer as well.

Line 22: float rightNow = Time.realtimeSinceStartup;

  • The variable rightNow is declared and assigned the current elapsed time
  • So we have futureTime with a value of 8 seconds into the future, and rightNow with a value of the actual seconds right now, which is updated constantly
  • The value is updated constantly because the line of code is in the StateUpdate() method which is called constantly

Line 23: countDown = (int)futureTime – (int)rightNow;

  • The difference is stored in the variable countDown which was declared on line 10
  • Since rightNow is constantly updated, so in 8 seconds the values in futureTime and rightNow will be equal to each other
  • Therefore, the value in countdown starts at 8 and decreases to 0, in 8 seconds

    Note

    Notice the (int) in front of each variable. The two variables are of type float, while countdown is of type int. I want it to be of type int when used later in line 38. The (int) type takes the float value, strips off the decimal part of the number, and leaves just an integer. A float is being cast (changed) to an int.

Line 25: if(Input.GetKeyUp(KeyCode.Space) || countDown <= 0)

  • This line was modified to check two conditions using an OR operator
  • When countDown reaches 0, this condition becomes true
  • The code block is executed to switch to the PlayState

Line 27: Switch();

  • The code that used to be here was moved to the new Switch() method on line 41
  • This line calls the switch() method
  • The code flow now jumps to line 43

Line 43:: Time.timeScale = 1;

  • This line assigns the value 1 to timeScale
  • Now the time in the game is back to normal
  • Remember, we had set it to 0 on line 17

Line 44: manager.SwitchState (new PlayState (manager));

  • This is the same switching code we always had to switch to PlayState, it's just in this Switch() method now
  • Code flow now returns to line 27 where Switch() was called

Line 27: }

  • The code block is finished

The following is the code flow of BeginState GUI:

Line 33: if (GUI.Button(new Rect(10, 10, 150, 100), "Press to Play"))

  • Look up GUI.Button in the Scripting Reference
  • It returns a Boolean (bool) of true when clicked
  • Therefore, we use an if statement to detect when it's clicked
  • The Button() method has the following two parameters:
    • A Rect object (rectangle) is created which specifies its position on the screen and its size
    • The string text that will be on the button

Line 35: Switch();

  • This line calls the Switch() method on line 41

Line 38: GUI.Box (new Rect (Screen.width – 60,10,50,25), countDown.ToString());

  • The Box() method creates a visible rectangle box in the Scene to display the timer counting down
  • Because I wanted the box on the right side of the screen no matter what resolution the screen may be, Screen.width provides the pixel position of the right side of the Scene. Then I moved 60 pixels to the left of the right edge of the Scene.
  • The 60 mean: from the last pixel on the right, come back 60 pixels to the left, and that's where to start drawing the rectangle box.
  • The countDown.ToString() method takes an integer value stored in countDown and converts it to a string value.
  • It converts it to the string value because the second parameter of the Box() method requires a string type of value.

Considering how much is going on in the Scene, and that there are three ways to change to PlayState, there really isn't very much code in BeginState.

When you know the syntax, how to use variables and methods, and the concept of working with objects, coding is actually quite simple to learn because you're doing the same types of thing over and over. The most difficult part of coding is trying to discover all the features that Unity provides in the Scripting Reference, and how they work.

You could get to know C# to the point where you dream in code, but then you have to apply that coding knowledge to Unity. So you look in the Scripting Reference and think what is a EulerAngle or a Quaternion? What takes time is doing the research, and not necessarily writing the code.

Have a go hero – changing the State switching order

Assuming that BeginState will appear when the game starts and again after you lose, change WonState to switch to PlayState instead of starting over with BeginState.

Note

Hint: you have to change only one word in the WonState class.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.118.12.157