Hi, I’m still very green on the whole scripting front, and I’m having trouble with something that seems like it should be very basic. I’m trying my hand at a simple adventure, and I want to make an RPG-like text box, to display some dialogue. The game would wait for the player to press space to show the next line of dialogue, and when the dialogue is done the game would continue. During dialogue, the player should not be able to move and the game should freeze.
I cannot find a way to make this work. I’ve tried several iterations and all of them fail dramatically in different ways. I call cinematic1Intro() in the Start function:
Attempt number 1: Direct approach
I enter a while loop with a true condition and wait for a key press to change it. If this code is in, the game won’t even launch at all:
void cinematic1Intro()
{
bool cont = false;
text.text = "Hello and welcome!";
while (!cont)
{
if(Input.GetKeyDown(KeyCode.Space))
{
cont = true;
}
}
cont = false;
text.text = "Feel free to explore.";
while (!cont)
{
if (Input.GetKeyDown(KeyCode.Space))
{
cont = true;
}
}
text.text = "";
cinematic = false;
}
Attempt number 2: Coroutines
Based on the answers in this thread , I tried to use coroutines to stop the flow of the execution, but when running the game everything gets executed directly so the texts just overwrite each other and the game continues.
void cinematic1Intro()
{
text.text = "Hello and welcome!";
Debug.Log("This is ");
StartCoroutine(waitForSpace());
text.text = "Feel free to explore";
Debug.Log("pissing me out.");
StartCoroutine(waitForSpace());
text.text = "";
cinematic = false;
}
IEnumerator waitForSpace()
{
//wait for space to be pressed
while (!Input.GetKeyDown(KeyCode.Space))
{
yield return null;
}
}
Attempt number 3: Coroutines, but different.
Different version, but does the same thing as 2.
void cinematic1Intro()
{
StartCoroutine(waitForSpace("Hello and welcome!"));
StartCoroutine(waitForSpace("Feel free to explore"));
StartCoroutine(waitForSpace(""));
cinematic = false;
}
IEnumerator waitForSpace(string textToShow)
{
text.text = textToShow;
//wait for space to be pressed
while (!Input.GetKeyDown(KeyCode.Space))
{
yield return null;
}
}
I’m starting to feel like I will need to create a new scene just to show a few lines of dialogue and then move the player to a scene with the game on it, and do this every time I want to show dialogue (which can get unwieldy, as this is a dialogue heavy game). I am sure it cannot be so complex and I must be missing something obvious.
Thanks in advance!