The State[] variable

ok so im pretty sure i understand but its taking a second for it to wrap around my head. so the only way for us to change the state from our adventure script is to make a new variable which consist of what the state script returns when its function is called? ive written in code my understanding if im incorrect plz let me know.

    private void manageStates()
    {
        print(state);

        if (Input.GetKeyDown(KeyCode.Alpha1))
        {
            //A new variable of type array is made by the return function of our state classes array
            State[] nextState = state.getNextStates();
            //we then are able to use this variable in its array format to choose which state is next
            state = nextState[0];

            textComponent.text = state.getStateStory();
        }
    }

Hi Jayy,

//A new variable of type array is made by the return function of our state classes array

We create a new variable and assign to it the array that is returned by the getNextStates method. The variable is null by default unless you assign something to it.

//we then are able to use this variable in its array format to choose which state is next

When the getNextStates method returned an array with at least one element, we can access the first element via the nextState variable and by adding [0].

yeah i think i get it now. Also. im trying to set images at certains states in my game. how do i check what my current state is?

What do you mean by checking what the current state is?
You assign a State object to your state variable, and that’s the “current state”. If you have a sprite in your State object, you can return it in a similar way to what you did with the Text. And you can receive the sprite via the state variable.

so right now i know how to change the background image of my game at will like on a key press for example but basically what im trying to do is this

if(state == state of my game where im on the beach )
{
//change background image to blank
}

i just dont know how to check aka if == what the current state is

1 Like

Why don’t you simply expose an image in your State instance and assign an image to its Inspector? You really don’t want to hard-code stuff in the AdventureGame.

Add the beach image to your beach State and have the AdventureGame instance call state.GetImage or something like that.

nah id rather do it like that if its possible. its easier to keep track of than attaching backgrounds to the states if i already know what state im in. If im thinking of what youre saying is correct then id have to go and add a background image to every single one of my states which in some cases can be good but its not necessary.

Lots of things are possible but not necessarily best practice. You could compare the name of the state object to a string, e.g.

if (state.name == "beach")
{ 
    // assign beach image to the Image component
}
2 Likes

says caant convert string to bool.
edit wait nvm i did = instead of == going to see if it works now.

worked perfectly. Thank you nina :smiley:
wish i made this another thread for other people incase they wanted to know.

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

Privacy & Terms