Delay User Input
I'm creating a text based game using a typewriter effect, while the text is typing the user can type during this time and their text appears as soon as the typewriter text is complete. Is there a way to stop user input until the typewriter effect is over. I'm not even sure how to word this on google
My code:
https://pastecode.io/s/msf4a3ef
26 Replies
A pointer to the relevant code section? little bit much to dig into.
Well i guess thats kinda where my issue lies. to preface i am a complete noob, just started a week ago so if i dont understand or answer your question correctly, i am sorry! I am not really sure where to point you to or how to search on google for a solution. the bottom of the code has my functions, i have a typewriter effect function, the text from the evil robot appears like a typewriter, which means during that time that the text is displaying, the player can type but will only see their text once the typewriter effect is over. while the typewriter effect is happening, i do not want the player to be able to input any text. with that said, if it makes any sense, what can i pinpoint you to in my code?
It's the terminal that buffers inouts and sends them as soon as the thread is freed
Not sure you can do anything about it
Maybe you can somehow clear the buffer when the typing is done
hmmm thats unfortunate, i appreciate your help!!
Clear the screen and print everything in one go?
Maybe a bit hacky
well its dialogue between the "evil robot" and the player so i was hoping to keep all text on screen until they enter a new database in the game. i hope that makes sense
obs no one is playing this, i am just making this to learn but i was hoping to get this issue solved just so i know for the future.
I'd attempt to quickly clear the terminal and then print the entire conversation log
once the typewriter is over
Or just clear the line entered by the player
You'd have to use http://msdn.microsoft.com/en-us/library/system.console.setcursorposition.aspx and write an empty line
Console.SetCursorPosition(Int32, Int32) Method (System)
Sets the position of the cursor.
its not so much that their text is an issue but if they were to hit enter during a certain part before the typewriter finishes then they get the next task wrong on their first choice because they essentially are entering "blank"
does that make sense?
im looking into that right now! thanks!
This wouldn't prevent an enter though
well crap lol
still a good read! deff more tools to add to my toolbox
At that point it gets very hacky, try to detect when the first answer is too fast and ignore it?
just spitballing really
is there a way to set a bool istypewriteractive = true/false that would reflect false once the typewriter effect is over?
Clear the answer after the text is displayed and ask again
like i of course can set it to false in my function
but is there a way to recognize that the effect is indeed over?
your code is synchronous, it's over when it exits the function
This should work for asynchronous code
Doubly so for synchronous
let me look up what that means, ill be back! thanks again
Means you code happens line by line. Whatever comes after the function call doesn't happen until the function is done.
Nothing really happens until it's done
is the function still being called while the effect is still happening?
You can create a keyboard hook event handler and maybe just eat all keystrokes that happen while you are typing, and then have the handler pass the strokes along when it switches back to the other mode...might be a little more involved than you want but it should definitely be possible I think.
I made progress with asynchronous code but now i have a new issue, the first letter of text is the correct color but then it switches to green. the white text is the robot talking and the green is player input, well thats how its supposed to be..when i added the new code it fixed the original issue but now i have a color issue, back to the drawing board. I APPRECIATE EVERYONES HELP SO MUCH! THANK YOU!
or wait, now if i hit enter during the typewriter effect, it skips letters lol...i think ill remove that code for now and just keep working on the basics, no one is playing this game, just doing it for fun, i know not to hit enter lol
You might look at using Spectre.Console if you want to do colorized text, etc.
Spectre.Console - Welcome!
Spectre.Console is a .NET library that makes it easier to create beautiful console applications.
I will add it to my list! thank you!!!!