[QUESTION] Switch statements, and additional scripts/text boxes?

Hi,

I’m probably jumping the gun somewhat here, but I do have a couple of questions…

The first, is there a reason why Switch statements where not introduced/used? I’ve always been lead to believe they are faster/more efficient to process than nested if statements.

The other question is about adding additional text boxes.

I hit on the idea of having a second text box to display the key press options. Mostly for display/formatting reasons. However, when I attempt to control it with the same script as the first text box it just duplicated the text already being shown.

I believe I need a new script to control the contents of this second text box, but it will need to know the current state in order to display the correct options. How do I see the current value of myState from another script?

Thanks

Okay, I think I found my answer to the 2nd question here…

http://answers.unity3d.com/questions/10857/how-can-i-access-other-scripts-and-their-functions.html

This makes sense to what we have learned so far, and appears to work. I can now have two independent text boxes and control them with the same core myState variable. Excellent!

So, I spent about 4 hours today hammering to try and figure out the Switch command and never came up with anything. I don’t think it’s possible, at least at this stage of the course. A switch statement should in theory work for the Update function. So instead of if (myState == States.cell) you could use a switch statement. I haven’t tested it though.

Here is everything I went thru trying to turn the individual functions for the rooms into switch statements instead of if else statements.

The Input.GetKeyDown command is a Boolean value, so what it returns is a yes or no based on what you tell it to test for. Hence the compile errors I kept getting.

I first tried it using in the following format to no avail, as method group could not be converted to a int, char, or string.

switch (Input.GetKeyDown) {
case KeyCode.S:
}

I then tried it like so, where instead of doing the case on the KeyCode, I just tried the key.

switch (Input.GetKeyDown) {
case “S”:
}

Same issue, can’t convert the method type.

I then tried changing tactics, and instead of using Input.GetKeyDown, I tried using Input.inputString. Success! I could compile my code! But, once the game was running, no matter what input I entered into the game, it never changed off of the first screen. It was like it wasn’t actually taking in any input.

So, I went and reviewed the Input.inputString, and it looks like it is supposed to exit out of the method after an enter key. Tried that in the game, and it did not take me anywhere. I then attempted to create a string variable called choice. After displaying my prompt, I then attempted to set my string variable to the method. E.G.

string choice;
choice = Input.inputString;

While the code compiled, still same issue. The switch didn’t seem to be performing any tests, and I didn’t actually seem to be getting any input from the Input.inputString command. That was where I left my tinkerings before doing it the way the class told me to do it originally.

1 Like

Hi Kyle,

Despite all of the awesomeness Unity includes I am often amazed that something which appears to be seemingly required doesn’t have better/easy support.

Your observations regarding Input.GetKeyDown are correct, a boolean value will be returned depending on the KeyCode or String that you pass as a parameter to be checked. As such, trying to compare true to S is never going to do what you want it to do.

Your next approach is a good one, however, Input.inputString can, in fact, return more than just the one character!

Try something along the lines of the following, perhaps create yourself a new, empty scene (so that I don’t break your cool stuff)…

/// <summary>
/// Update is called once per frame
/// </summary>
private void Update()
{
	CheckForPlayerInput();
}


/// <summary>
/// Checks for keyboard interaction
/// </summary>
private void CheckForPlayerInput()
{
	if (Input.anyKeyDown)
	{
		string input = Input.inputString;

		if (input.Length > 0)
		{
			input = input.Substring(0, 1);
		}

		OptionSelected(input);
	}
}

In the above, the CheckForPlayerInput() method is being called from within the Update() method.

The CheckForPlayerInput() method first checks to see if Input.anyKeyDown is true, e.g. has anything been pressed. If not, we just pass over the following code. If a key has been pressed then we grab the Input.inputString and pop it into a variable.

Next, we check that the length of the variable is at least 1 character long and then use the Substring() method to take the first character. The 0,1 refer to the start and end character positions to return the substring from, in this case, 0 and 1 - thus, the first character.

Note: The above does assume that, in the case of a player hitting more than one key in the same frame, the first key hit was the intended one. You could, of course, opt to seek clarity from the player, e.g. if the length of the Input.inputString was greater than 1 then prompt the user to be a bit more careful with their sausage fingers :slight_smile:

Finally, we pass the single character to the OptionSelected() method. So, let’s have a look at that…

Note: You’ll only get ASCII characters returned in the inputString, it may also contain two special characters, backspace and return/enter (see the link to the documentation below for details).

/// <summary>
/// Handles option selection
/// </summary>
/// <param name="key">The key which was pressed</param>
private void OptionSelected(string key)
{
	key = key.ToUpper();

	switch(key)
	{
		case "R":
			Debug.Log("Rob was 'ere!");
			break;
			
		case "K":
			Debug.Log("Kyle was 'ere!");
			break;
			
		case "C":
			Debug.Log("Coding Coolness!");
			break;

		default:
			Debug.Log("Stop pressing keys I don't care about!");
			break;
	}
}

The first thing we do in the above is upper-case the character that has been passed to this method. It doesn’t really matter if you upper-case it, or lower-case it, but what we want is some form of consistency that we can rely on (helps cover scenarios where a user may have CAPS LOCK enabled for example).

Then, we just use a switch statement to act upon the specific character.

Hope the above is of use to you.


See also;

2 Likes

Privacy & Terms