Understanding the code

Hi, I am very new to programming. I was able to complete this video but I don’t fully understand what is happening in this line of code:

var nextStates = States.GetNextState();

Can you walk me through it?

Hello jpruett,

var nextStates
First you are creating a var which is in State class (You have created this class as a scriptable object before.)

States.GetNextState();
And after you are calling GetNextState() function (which you have created this function in States scriptable object as well.)

You are equalizing that whatever that function returns (as a State) to nextStates variation.

Okay, I follow all of that. I guess I should have asked my question more specifically, but at the time I wasn’t sure exactly how to do that.

The part I’m having trouble understanding is “States.GetNextState();”

My understanding is that what’s after the dot “belongs to” what is before the dot, sort of like nesting folders. However, in this case, States is a variable of type State, so I don’t understand how the method GetNextState() can be a subset of the States variable. Is that not what’s happening here?

Thanks in advance :slight_smile:


Virus-free. www.avg.com

Okay, I think I got your point.

The dot belongs to State class. Think of you are calling State class. And state class ask you that what should I do?
And you simply tell him to get the function that you wrote before. You tell him GetNextState(), so he is trying to get whatever you wrote in GetNextState() function.

In other words, GetNextState () is something that you wrote. You can wrote callnextstate() or anything like it in that scriptable object. So it can call whatever it is.

I hope the explanation is clear.

Okay, that also makes sense, and is what I expected.

However, I don’t have State.GetNextState(). I have States.GetNextState().

I have the class State.cs but I also have this bit:

void Start () 
{
    States = startingState;

    // ...
}

Then, down below I have

private void ManageState()
{
    var nextStates = States.GetNextState();

    if // ...
}

the var and the States are both of type States[array].

If I use State.GetNextState() (instead of States.etc) I get the following error message:

An object reference is required for the non-static field, method, or property ‘State.GetNextState()’


Virus-free. www.avg.com

Your issue in the above is that you’ve named the member variable differently to the course. It wasn’t States it was State.

public class AdventureGame : MonoBehaviour
{
    private State state;

    // ...
}

It doesn’t matter that you named your variable differently, as long as you then use that name where required;

private void Start()
{
    state = startingState;

    // ...
}
private void ManageState()
{
    State[] nextStates = state.GetNextStates();

    // ...
}

Also, pay close attention to your use of case. Variables typically use camelCase where as classes, properties and method are typically PascalCase. If you start mixing these around you’re going to find it a lot harder to tell which refers to a type and which is a variable name.

Couple of links for you below, one specifically regarding the dot operator you were asking about above.

Hope this helps :slight_smile:


See also;

Okay, now I think I understand. I wasn’t connecting the dots between the state variable and that it is of Class type State. Variables types ARE classes. Is that correct?

1 Like

Often, but not always, they could also be a struct or an enum for example :slight_smile:


See also;

Perfect, thanks for the help!

You’re very welcome. :slight_smile:

If you feel your query is now resolved, please indicate the reply which solves the problem for you.


See also;

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

Privacy & Terms