- Home /
Checking Input on 2 different coroutines at the same time
Hi, this is my problem: I have 2 different coroutines that are "waiting" to check when the player presses the keys "1" or "2" of the keyboard. Is a Conversation system, so, the ConversationController is waiting for the keys 1 or 2 to continue the next string of the dialogue, while other script containing the dialogue, is waiting for the player to press 1 or 2 in order to choose the option to say.
I found the problem that the player must press 2 times the key; To choose the option and to continue the dialogue.
The issue could be solved by using Input.GetKeyDown () on the ConversationController, and Input.GetKeyUp () in the other script.
But I do not really like that solution. I want to understand why 2 cueroutines can not check at the same time Input.GetKeyDown
Thanks for reading and helping :)
Coroutines does not run simultaneously meaning that they run on the $$anonymous$$ain thread. The difference from normal functions is that they ll return control to the caller when a yield return instruction encountered and re gain control after the instructed time or codition has passed. By putting the checks on the Update() of the active dialogue it will only check while the dialogue is Active. There is no performance issue and might occupy less memmory from instatiating 2 coroutines (that depends on several factors though).
Answer by IvanDrake · Feb 04, 2017 at 12:01 AM
Why are you using two Coroutines? Why not use the Update function? You shouldn't need Coroutines to "wait" for input.
if ( Input "1"){
// Select option 1
// Advance dialog
} else if ( Input "2"){
// Select option 2
// Advance dialog
}
Hi, thanks for your answer
I'm using coroutines because it's more efficient than checking all the time in the update function. I just need to wait for input on certain moments, when there is a dialogue with options, so checking every frame is not necessary.
Admittedly, I'm not a program$$anonymous$$g expert, but in my experience, using the Update is typically less processor heavy than using multiple, simultaneous coroutines.
If you don't need it checking for input every time, then use a Boolean:
if(ready){
if(Input "1") ... etc.
You can always start your coroutines from inside the If statement, if necessary.
Of course, I've never written a dialog tree before, so I'm not entirely sure how you're script is working.
Your answer
Follow this Question
Related Questions
Input management with coroutines in a fighting game. 0 Answers
Unable to catch KeyCode.LeftShift Down AND Up 5 Answers
Input keycode string not working 2 Answers
Help In Making a SphereCast for 3D Tire! Working RayCast Script included! 0 Answers
Detecting left and right being pressed at the same time. 1 Answer