Trying To Set Values Incrementally

Hello everyone, I have no idea how we’re supposed to let the user assign the values in a convenient fashion. It seems… awkward to say the least. I spent hours looking for a solution but nothing seemed easy to use. But, I figured I’d give it a try anyway by allowing players to incrementally alter the minimum and maximum values. I’ve run into trouble though, for whatever reason my game won’t even start when I try to use the NUMPAD+ and NUMPAD- buttons to alter the values, could someone help me understand why?

It’s in the SetValues() function, at least that’s where the code that’s supposed to run is. Click the spoiler below to look at the code.


[spoiler]using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class NumberWizard : MonoBehaviour {
//Do the rubber duck
// Use this for initialization
int max;
int min;
int guess;
int playing;
int setting;

void Start ()
{
	SetValues();
}

void StartGame()
{
	playing = 1;
	setting = 0;


	print("==========================");
	print ("Welcome to Number Wizard");
	print ("First, decide on a range of numbers. It can be any range you want, but only whole numbers.");
	print ("Type the numbers for your minimum value here: ");

	print ("Pick a number in your head, but don't tell me!");


	print ("The highest number you can pick is " + (max));
	print ("The lowest number you can pick is " + (min));

	print ("Is the number higher or lower than " + guess + "?");
	print ("UP = higher, DOWN = lower, and RETURN = equal.");

	max = max +1;
}

//PLAYING
void NextGuess () {
		guess = (max + min) / 2;
		print ("Higher or lower than " + guess);
		print ("UP = higher, DOWN = lower, and RETURN = equal.");
}

//SETTING THE MINIMUM AND MAXIMUM VALUES BEFORE PLAYING
void SetValues ()
{
	playing = 0;
	setting = 1;
	print ("Let's set your minimum and maximum value. To start, press NUMPAD+ to set your Maximum Value or NUMPAD- to set your minimum value. If you want to start the game, press SPACE."); 
	if (Input.GetKeyDown (KeyCode.KeypadMinus)) {
		SetMin();
	} else if (Input.GetKeyDown (KeyCode.KeypadPlus)) {
		SetMax();
	} else if (Input.GetKeyDown (KeyCode.Space)) {
		StartGame ();
	}
}

//SET MINIMUM
void SetMin ()
{
	while (setting == 1) {
		print ("Press and hold the UP and DOWN arrowkeys to set your values, press NUMPAD+ to set the maximum value. Press SPACE to start the game.");
		if (Input.GetKeyDown (KeyCode.DownArrow)) {
			min--;
			print ("Current Minimum Value: " + (min));
		} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
			min++;
			print ("Current Minimum Value: " + (min));
		} else if (Input.GetKeyDown (KeyCode.KeypadPlus)) {
			print ("Final Minimum Value: " + (min));
			SetMax ();
		} else if (Input.GetKeyDown (KeyCode.Space)) {
			StartGame ();
		}
	}
}

//SET MAXIMUM
void SetMax ()
{
	while (setting == 1) {
		print ("Press and hold the UP and DOWN arrowkeys to set your values, press NUMPAD+ to set the maximum value. Press SPACE to start the game.");
		print ("Next, let's set your maximum value."); 
		if (Input.GetKeyDown (KeyCode.DownArrow)) {
			max--;
			print ("Current Maximum Value: " + (max));
		} else if (Input.GetKeyDown (KeyCode.UpArrow)) {
			max++;
			print ("Current Maximum Value: " + (max));
		} else if (Input.GetKeyDown (KeyCode.KeypadMinus)) {
			SetMin ();
		} else if (Input.GetKeyDown (KeyCode.Space)) {
			StartGame ();
		}
	}
}




// Update is called once per frame
void Update ()
{
 while (playing == 1){
	if (Input.GetKeyDown (KeyCode.UpArrow)) {
		//print ("Up key was pressed.");
		min = guess;
		NextGuess();
	} else if (Input.GetKeyDown (KeyCode.DownArrow)) {
		//print ("Down key was pressed.");
		max = guess;
		NextGuess();
	} else if (Input.GetKeyDown (KeyCode.Return)) {
		print ("I won!");
		StartGame();
	}

}

}
}

//below code used to be in StartGame()
/guess = 500;
max = 1000;
min = 1;
/[/spoiler]

Hello Dan!

I think the problem comes from the fact that you call SetValues() only in the Start function. What happens when you try to run the game is that the Start function is called only at the very beginning, and then it’s the Update function that takes the lead. Then the update function is called approximately 60 times per seconds (the fps). I agree it is a bit tricky, especially if you have previous coding experience unrelated to any game engine (at least that’s how I felt, I spent hours like you trying to figure this out).

So back to your code, when you call SetValues in the Start function the following happens:

  • playing is set to 0
  • setting is set to 1
  • you print the sentence (“Let’s set…”)
    but then, unless he or she has superhuman reflexes, the user don’t have time the press any key. The program simply assumes that no key was pressed, and then goes in the update function. A very simple way to convince yourself of this, is to print something in the Update function. For instance, if you add print(“hello world!”) at the beginning of your Update function, you will see in your console that the sentence is being printed multiple times per second.

Therefore, setting the minimum and maximum should be handled in the Update function.

To do so you are right to use variables playing and settings. By the way, I would recommend that you declare them as booleans

bool setting=True; //or False...

It makes their purpose easier to understand and consumes less memory (though for this simple program it is definitely not an issue).

However, you will run into troubles if you use while loops in the update function. You risk having the program freeze (I am not sure exactly whym I would say it is related to the fact that the Update function is supposed to be called every frame, and thus such while loop will be treated as infinite). Instead of loops, use conditions

if (settings){...}

This way, you could have in the Update function a part starting with if (setting) during which you set min and max, and part if (playing) for the game mode.

Finally I just share a link to a older post. The person who wrote this answer shared his code regarding how to set the minimum and the maximum, and also gave a good pointer to a C# documentation page that handles the question of user input through the console.

I hope this helps you solve your problem! :slight_smile: