TextController not all routes working

I was following the text adventure tutorial and seem to have got the code copied correctly it has no errors and I don’t see any mistypings but part of the script will not run properly. Once I have picked up the mirror if I press the button to check the sheets again it will change to the state but not perform any code in the state’s function, it will then freeze and no longer accept any inputs so I have to use the mirror to open the door straight away or the program breaks. I could remove the option to go back in the first place but it would cut out part of the game. Here is my code

	public Text text;
	private enum States {cell, mirror, sheets_0, lock_0, cell_mirror, sheets_1, lock_1, freedom};
		private States myState;

	void Start () {
		myState = States.cell;
	}

	void Update () {
		print (myState);
		if (myState == States.cell) {
			state_cell ();
		} else if (myState == States.sheets_0) {
			state_sheets_0 ();
		} else if (myState == States.lock_0) {
			state_lock_0 ();	
		} else if (myState == States.lock_1) {
			state_lock_1 ();
		} else if (myState == States.mirror) {
			state_mirror ();
		} else if (myState == States.cell_mirror) {
			state_cell_mirror();
		} else if (myState == States.freedom) {
			state_freedom();
		}
	}

	void state_cell () {
		text.text = "You are in a prison cell, and want to escape. There are" +
			"some dirty sheets on the bed, a mirror on the wall, and " +
			"the door is locked from the outside. \n\n Press S to " +
			"view Sheets, M to view Mirror, L to view Lock";
		if (Input.GetKeyDown (KeyCode.S)) {
			myState = States.sheets_0;
		} else if (Input.GetKeyDown (KeyCode.M)) {
			myState = States.mirror;
		} else if (Input.GetKeyDown (KeyCode.L)) {
			myState = States.lock_0;
		}
	}

	void state_mirror() {
		text.text = "The dirty old mirror on the wall seems loose.\n\n" +
		"Press T to Take the mirror, or R to Return to cell";
		if (Input.GetKeyDown(KeyCode.T)) {
			myState = States.cell_mirror;
		} else if (Input.GetKeyDown(KeyCode.R)) {
			myState = States.cell;
		}
	}

	void state_sheets_0 () {
		text.text = "You can't believe you sleep in these things. Surely " +
		"it's time somebody changed them. The pleasures of " +
		"prison life I guess! \n\n Press R to Return to " +
		"roaming your cell";
		if (Input.GetKeyDown(KeyCode.R)) {
			myState = States.cell;
		}
	}

		void sheets_1(){
		text.text = "Holding a mirror in your hand doesn't make the sheets look " +
		"any better.\n\n" +
		"Press R to Return to roaming your cell";
		if (Input.GetKeyDown(KeyCode.R)) {
			myState = States.cell_mirror;
		}
	}
	
	void state_lock_0() {
		text.text = " This is one of those button locks. You have no idea what the " +
					"combination is. You wish you could somehow see where the dirty " +
					"fingerprints were, maybe that would help.\n\n" +
					"Press R to Return to roaming your cell";
				if (Input.GetKeyDown(KeyCode.R)) {
					myState = States.cell;
				}
		}
		
	void state_lock_1() {
		text.text = "You carefully put the mirror through the bars, and turn it round " +
					"so you can see the lock. You can just make out fingerprints around " +
					"the buttons. You press the dirty buttons, and hear a click.\n\n" +
					"Press O to Open, or R to Return to your cell";
		if (Input.GetKeyDown(KeyCode.O)) {
			myState = States.freedom;
		} else if (Input.GetKeyDown(KeyCode.R)) {
			myState = States.cell_mirror;
		}
	}

	void state_cell_mirror() {
		text.text = "You are still in your cell, and you STILL want to escape! There are " +
					"some dirty sheets on the bed, a mark where the mirror was, " +
					"and that pesky door is still there, and firmly locked!\n\n" +
					"Press S to view Sheets, or L to view Lock" ;
		if (Input.GetKeyDown(KeyCode.S)) {
			myState = States.sheets_1;
		} else if (Input.GetKeyDown(KeyCode.L)) {
			myState = States.lock_1;
		}
 }

	void state_freedom() {
		text.text = "You are FREE!\n\n" +
					"Press P to Play again";
		if (Input.GetKeyDown(KeyCode.P)) {
			myState = States.cell;
		}
	}

	//breaks on lock1 state. won't recognise sheets_1 input, only works if first input is open door to freedom.
}

EDIT: When I remove the code that should take me back to the sheets state then the code below no longer works meaning the lock function doesn’t work when I press open.

Hi @Brocolli,

In your method state_cell_mirror(), based on the key press of either S or L you set your specific states, States.sheets_1 or States.lock_1.

In the case of pressing S, you set the state but your Update() method does not perform any check for that state.

	if (myState == States.cell) {
		state_cell ();
	} else if (myState == States.sheets_0) {
		state_sheets_0 ();
	} else if (myState == States.lock_0) {
		state_lock_0 ();	
	} else if (myState == States.lock_1) {
		state_lock_1 ();
	} else if (myState == States.mirror) {
		state_mirror ();
	} else if (myState == States.cell_mirror) {
		state_cell_mirror();
	} else if (myState == States.freedom) {
		state_freedom();
	}

As such, you never get to your method void sheets_1(), which, for consistency, you may consider renaming also to void state_sheets_1().

Hope this helps.

Thanks alot man, helps having someone else read it when it’s hard to notice your own mistakes

1 Like

Yep, code blindness sets in after a while, it will not matter how long you stare at it you will convince yourself you are right and the computer is wrong :smiley: We’ve all been there :slight_smile:

With this specific game one thing that may help you going forward, especially if you choose to extend it further, would be to create a little check list…

  1. Add new States enum
  2. Add additional logic to the Update() method using the new State enum
  3. Add a new method for the new State

The reason I suggest this is because when you first add a new enum, nothing is going to error or give you any visual indication that it isn’t used. So if you add 20 in one go for example, it gets a little harder to see which you have written code for and which you haven’t.

Adding the logic to your Update() method next ensures that the enum is used, it will also make you think about the name of the method you will need to create for that new State, when you type that in, the IDE is going to moan at you because you don’t have that method yet, now you have a visual indication that you need to create it.

Finally, add your new method, the visual indication that it was missing in the IDE will now disappear.

Once you have that, you can repeat the process as many times as needed and you are less likely to have bits missing, or, bits that aren’t used.

Hope this helps :slight_smile: