Chapter 7. Creating the Gameplay is Just a Part of the Game

Gameplay is usually the main thing everyone thinks of when first undertaking the creation of a game. Build a scene, fill it with GameObjects, and write the scripts. After the game is well along in its development, a realization begins to appear that there's more to a game than just playing it. Putting a game on the market means it has to include things such as beginning title screen, the game options, or a player settings menu. Also, what happens when the user wins the game, or loses the game? It's one thing to write the code for GameObjects, but what about the code to add on all the other parts that come before and after the actual playing of the game?

In this chapter, we'll begin work on a C# State Machine, one possible solution for providing the necessary base code to easily incorporate the non-playing parts of a game.

In this chapter we will discuss the following topics:

  • How a State Machine helps in making a game easier
  • A logic overview of the different States
  • The base code for creating the State Machine

So let's start to glue things together.

Applying your new coding skills to a State Machine

You learned the fundamentals of C# coding in the previous six chapters which were used for creating Components. You will use the same coding fundamentals to create a State Machine. You have learned in Chapter 6, Using Dot Syntax for Object Communication, that an object created from a class is called an instance of the class. In Unity, an object created from a class is called a Component. These terms may be different but they mean exactly the same; an object is created as defined by a class.

However, there is a difference in the way the traditional C# environment of the State Machine gets an instance object created, and the way Unity gets a component object created. The difference is as follows:

  • A class we write for Unity has to be attached to a GameObject.
  • A class we write for the State Machine is not attached to a GameObject.
  • Unity hides the code that is necessary to create an object. Unity knows to create the object because it's attached to a GameObject.
  • For the State Machine, we have to write the code that is necessary to create an object.

Note

Have no fear! It's very similar to the code in Chapter 5, Making Decisions in Code, we used to create a List and Dictionary object.

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

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