Waiting for keypress from user (how to make a co-routine?) C#
Hi,
So after doing some reading and checking out everyone else's code, I'm still unsure of how a co-routine should be constructed. I noticed many people do this with a while loop. But I have a for loop that I would like to do this with, I need it to stop at the if statement and check to see what the user pressed. Suggestions?
void LetterChecker() {
for (int i = 0; i < gCharArray.Length; i++) {
string charHolder = gCharArray[i].ToString();
if (Input.GetKeyDown(charHolder)) {
print(charHolder);
}else {
print("Wrong Letter");
}
}
print("Word Typing Ended");
}
Answer by Jopan · Dec 15, 2015 at 03:55 PM
When using a coroutine to wait for specific input or actions, I like to do something like this.
IEnumerator MyCoroutine()
{
for (int i = 0; i < gCharArray.Length; i++)
{
string charHolder = gCharArray[i].ToString();
while ( !Input.GetKeyDown(charHolder) )
yield return null;
print (charHolder);
}
print("Word Typing Ended");
}
If you must check for the wrong key I would replace the while body with
IEnumerator MyCoroutine()
{
for (int i = 0; i < gCharArray.Length; i++)
{
string charHolder = gCharArray[i].ToString();
while ( !Input.GetKeyDown(charHolder) )
{
if ( Input.anyKeyDown )
{
print("Wrong Letter");
}
yield return null;
}
print (charHolder);
}
print("Word Typing Ended");
}
This works perfectly! Thank you. I will use the first variant. Although the 2nd one will be useful during some time. I tried it and even if I press the correct letter, "Wrong Letter" still shows up. I even added an && to the if statement to exclude the fact that I pressed the correct keystroke. $$anonymous$$ept considering my correct keystroke as incorrect.
Anyway, for now this will do. I'll figure out the other way in some time.
Glad it worked. $$anonymous$$aybe this will work for the second part.
IEnumerator $$anonymous$$yCoroutine()
{
for (int i = 0; i < gCharArray.Length; i++)
{
string charHolder = gCharArray[i].ToString();
while ( true )
{
if ( Input.any$$anonymous$$eyDown )
{
if ( Input.Get$$anonymous$$eyDown(charHolder) )
{
break;
}
else
{
print("Wrong Letter");
}
}
yield return null;
}
print (charHolder);
}
print("Word Typing Ended");
}
When I use this, I get a "Unreachable code detected" at the top of the for loop where i++ is. I thought that yield return null should only break the while loop, not the for loop?
Answer by Kimi073 · Dec 15, 2015 at 02:43 PM
This is basically one of the easiest way to make use of co-routine written in C#:
IEnumerator YourCoroutineName(){
// Your code that you want here
// In this case is the for function
}
void YourFunctionName(){
StartCoroutine("YourCoroutineName");
}
Of how to wait until the user to type then why don't you try while loop, may be it's the best and common way to use. Myself don't have any suggestion for this issue.
Can I yield without a co-routine? Or is it absolutely necessary?
I think you can though not sure. $$anonymous$$ost of the times I always use it in co-routine
Your answer
Follow this Question
Related Questions
MoveTowards and Lerp not working 1 Answer
StartCoroutine not listening to parameters 1 Answer
yield ends method 0 Answers
Stop previous coroutine and start new one from beginning 1 Answer
Weird problem with simple boolean and "if" statement [C#] 0 Answers