Programmatically Changing nextStates[]

I had a question regarding how to programmatically change the nextStates array. For instance, I have a scene where the player can look out their window to find a clue. However; I don’t want them to look out the window again and see the same thing. I made a public void method as outlined below, but I don’t know how to pass a new state as an argument.

public void SetNextState(int arrElement, State newState)
    {
        nextStates[arrElement] = newState;
    }

I've tried to call it in this manner:

List<string> knowledge = new List<string>();

if(state.name == "BedroomWindow2")
        {
            knowledge.Add("Mr. Bojangles was left behind.");
        }

        if (state.name == "Bedroom")
        {
            foreach (string fact in knowledge)
            {
                if (fact.Contains("Mr. Bojangles was left behind."))
                {
                    state.SetNextState(1, BedroomWindow3);
                }
            }
        }

Hi rLee,

If you change something in the scriptable object, that change will be persistent. This means that your nextStates array will not contain the elements that you deleted anymore when you restart the game.

If something happened, keep track of it in the AdventureGame class, not in the State class. In the State class, you create a List where you add all rooms that have been visited. In your State class, you define a new array with states that only pop up when a room has not been visited yet. Modify the GetNextStates method accordingly so it returns only the available states or options. Look up “method overload” on DotNetPerls.

Alternatively, you could hardcode everything as you did. Don’t do that in the State class, though. In the State class, define your story part, messages, your states and whatever you need but do not modify anything during runtime. Instead, filter the data in AdventureGame or in a method.

Hopefully, this helped. :slight_smile:

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

Privacy & Terms