About public abstract class State

Hi
State is a public abstract class and by definition an abstract class cannot be used to create objects (you cannot create an instance of it using the new keyword) and to access it it must be inherited from another class.
Then we create a public class StateMachine : MonoBehaviour
Inside StateMachine we create a private State currentState variable and use it to access the Tick() method inside the abstract class State.

As much as I know private State currentState is not an instance of State because instances are created using the new keyword and in case of abstract classes this is not even allowed.
And StateMachine does not inherit from State either.

So how can you access Tick() just by declaring a State currentState variable inside StateMachine?

Thank you

Hey @Guybrush, I’m no expert here but the way I understood it was this
Here is a definition of what an Abstract class is

An abstract class is a class that has been declared abstract and has the option to include abstract methods. They cannot be instantiated, but they can be used as subclasses.

In StateMachine.cs we have this

public abstract class StateMachine : MonoBehaviour
{
    public void SwitchState(State newState)
    {
        _currenState?.Exit();
        _currenState = newState;
        _currenState?.Enter();
    }
    private State _currenState;
    private void Update()
    {
        _currenState?.Tick(Time.deltaTime);
    }
}

The public method SwitchState takes an argument of type “State”. Or something that is a subclass of state. If you look at PlayerBaseState - it’s a subclass of “State”. Then all our other PlayerSates (FreeLook/Targeting/Attacking/etc) are inheriting from PlayerBaseState

public abstract class PlayerBaseState : State

public class PlayerFreeLookState : PlayerBaseState

public class PlayerDeadState : PlayerBaseState

public class PlayerTargetingState : PlayerBaseState

Having all our PlayerSatets be subclasses of State - gives up the ability to override or use
all the methods found in State.

As I said, I’m no expert on this, feel free to correct me if this is wrong.

1 Like

@mihaivladan Hi Mihai, thanks for your help. I am not an expert either so I have no idea what is going on in this situation.
I have never heard the term subclass before so I checked its definition. The definition says that a class that is derived from another class is called a subclass (child class). This confirms what I wrote before. In order to access an abstract class it must be inherited from another class. And that does not seem to be the case between the abstract class State and the public class StateMachine

Hey @Guybrush!

When you declare currentState to be a State variable, the code knows to look at the definition of State to figure out what methods the currentState object has. So it looks at currentState and says, “I know you’re a State, and all States have a method called Tick(). So, you must also have a method called Tick().” That’s why you can access Tick() by declaring currentState to be a variable of type State.

This is separate from the idea of creating a new instance of a class with the new keyword. You’re right that we don’t do that in the StateMachine class, and that is because StateMachine expects a State object to be passed to it after the State object has been created.

You might ask, “When is the State object created if you can never use the new keyword to create a new one?” Like you mentioned, since the State class is abstract, we can’t create a new instance of the class using the new keyword all together. Instead, we can create new instances of subclasses of the State class like @mihaivladan mentioned. There is a key idea of inheritance that says a subclass has an “is a” relationship with its base class. So that means, if we have a class called PlayerTestState that inherits or derives from a base class called State, then PlayerTestState is a State and it can be used anywhere where a State can be used. So, we can make a new PlayerTestState object, using the new keyword since PlayerTestState is not an abstract class, and use that object as a State object in methods that expect a State object parameter to be passed to them.

Hi @efficientsea thanks a lot for your answer, I appreciate it. In order to understand what’s going on in this case, I’ve been looking at intermediate/advanced C# courses and I’ve come across a subject called pointer. Is State currentState a pointer?

@Nina Hi Nina, how are you? We’ve been having a conversation about abstract classes and how they are used in this course. The course looks like it gets right to point. So I would like to ask if it could be possible to add a prerequisite chapter that covers the necessary intermediate/advanced C# concepts to would help follow this course. Thank you.

In C#, we use the term reference rather than pointer, but essentially yes. CurrentState will alway spoint to the currently active state, regardless of what that state is. With this, you can access any method which is found in the State class.

This means you can call currentState.Begin(), Tick(), or Exit(), but you can’t call methods specific to the class represented by the reference…

@Brian_Trotter Thanks for the explanation

This topic was automatically closed 20 days after the last reply. New replies are no longer allowed.

Privacy & Terms