Time for action – implementing an interface

We're going to create a C# interface and implement it in BeginState, but incorrectly to see some errors. Then we'll implement the interface correctly. Perform the following steps:

  1. In the MonoDevelop Solution window, right-click on the Code folder and select Add | New Folder.
  2. Name the folder Interfaces.
  3. Right-click on the Interfaces folder and select Add | New File.
  4. In the New File window, select General | Empty Interface.
  5. In the Name field at the bottom, enter IStateBase.
  6. Click on the New button to create the file.

In the MonoDevelop Solution window, your file structure will now look like the following screenshot:

Time for action – implementing an interface

Now edit IStateBase file as follows:

  1. Remove lines 1 and 2. As a result, namespace will move up to line 1.
  2. Modify line 1 to: namespace Assets.Code.Interfaces.
  3. In the IStateBase code block, add line 5: void StateUpdate();.
  4. Then add line 6: void ShowIt();.

The IStateBase file will now look like the following screenshot:

Time for action – implementing an interface

Now we're going to have the BeginState class partly implementing the IStateBase interface so that we can see how using an interface guarantees our State classes meet the minimum requirements for the State Machine.

Now edit the BeginState class as shown in the following steps:

  1. Add on line 2: using Assets.Code.Interfaces;.
  2. Press Return/Enter so that line 3 is blank.
  3. Modify line 6 to: public class BeginState : IStateBase.

    We have now partly implemented the IStateBase interface. BeginState should now look like the following screenshot:

    Time for action – implementing an interface
  4. Save your files.
  5. Switch back to Unity and notice that you get two errors in the Console.

We were just told by Unity that we messed up. There are two errors informing us that BeginState did not properly implement the two methods required by the IStateBase interface: StateUpdate() and ShowIt().

Note

This is exactly what we want the Unity to tell us. On line 6, we said we were going to use an interface to guarantee that all methods specified in the interface are included in our State classes. We didn't do it, so Unity let us know.

Now let's almost correct the code in the BeginState class:

  1. Add in the two methods as shown in the following screenshot. Notice that I'm misspelling the StateUpdate() method as StateUpdatee():
    Time for action – implementing an interface
  2. Save the file.
  3. Notice that Unity still let's us know that we're messing up.
  4. Correct the spelling (remove the extra e from StateUpdatee())
  5. Save the file.
  6. Click Play in Unity to verify that your code now works correctly.

What just happened?

The Console output should be exactly as it was before we created and implemented the IStateBase interface. Even though more methods were added to BeginState, there's nothing in the code blocks to execute, nor did we change StateManager to call the methods. Our focus was to show how to implement an interface, and how it guarantees that the interface methods for a class are included correctly.

Have a go hero – adding another method to the interface

In a later chapter, we will be adding a third method to IStateBase named StateFixedUpdate(). You may add it now if you wish. This will require you to implement the StateFixedUpdate() method in all States we create.

Pop quiz – using a State Machine for game control

Q1. What is the main reason to incorporate a State Machine into a game?

Q2. Since State classes aren't attached to GameObjects, how does the code in a State get executed?

Q3. How many States are allowed in a State Machine?

Q4. What should you use to guarantee that all States have the required code for the State Machine to operate properly?

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

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