We have achieved some basic gameplay. Now is the time to tie it all together. In this chapter, we will cover the following topics:
gameplay
loop?singleton
class?GameManager
events?Well done so far. You have added basic functionality like jumping, physics, and running to the PlayerController
object. We are definitely going in the right direction here. The next important step is writing a neat GameManager
class to help us control the game events like:
StartGame
GameOver
Restart
For basic games like Jake on the mysterious planet, it is a good practice to have one instance of the GameManager
running and controlling all main events in the game. The gameplay
loop is simply a journey from the gameplay start to the gameplay phase and the game over phase. Time to write some code!
Let's create a new C# script and call it GameManager
, and write the following code:
As you can see, nothing is very complicated. We wrote very simple methods to help us control the main game events. This script does nothing yet; however, please add it to the Unity Scene. Create a new game object call it GameManager
, and add the GameManager
component to it. We will use it in the future.
We won't test this code in Unity yet. Let's think what can be improved. Would it be nice to have only one method to control the main events in the game? Let's edit the code a little to have something like that.
We have an enum GameState
declaration in lines 4 to 8. What is enum
? If you check on Google, you will be probably be directed to the Microsoft documentation. From the C# documentation:
"The enum keyword is used to declare an enumeration, a distinct type that consists of a set of named constants called the enumerator list".
Yes, this is a massively overcomplicated definition. Let's forget it and make our own, simple one by defining what we can use enum
for.
Enum
is a set of magic constants you can control your code with. We can define few states stored in enum
and use it without risk of typos like with using the string. Lines 4 to 8 simply create a new enum
, which works like an object and can be passed to the method as a parameter. The rest of the code should be well easy for you to understand. SetGameState
is called with a certain GameState enum
value. Note that we are also storing the currentGameState
value for the future.
We have more code that allows us to control events in the game. Now, we need to make some real use of it. If you press Play in Unity, you will see that nothing has really changed. Jake still runs the same way he did before GameManager
was added. This is because nothing actually changed in the PlayerController
class. PlayerController
and GameManager
are two separate instances of two different classes. They won't affect each other's behavior until we write some additional code.
We have already seen how to access components from different scripts by creating public member variable and dragging the game object to the field in Inspector. It is a good approach for this situation. However, I want to show you something better. If there is only one instance of the object required in the project, we can use the singleton approach.