C
C#3mo ago
SuckAToad

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
Lisa
Lisa3mo ago
A pointer to the relevant code section? little bit much to dig into.
SuckAToad
SuckAToad3mo ago
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?
Angius
Angius3mo ago
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
SuckAToad
SuckAToad3mo ago
hmmm thats unfortunate, i appreciate your help!!
Lisa
Lisa3mo ago
Clear the screen and print everything in one go? Maybe a bit hacky
SuckAToad
SuckAToad3mo ago
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.
Lisa
Lisa3mo ago
I'd attempt to quickly clear the terminal and then print the entire conversation log once the typewriter is over
Angius
Angius3mo ago
Or just clear the line entered by the player
SuckAToad
SuckAToad3mo ago
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!
Lisa
Lisa3mo ago
This wouldn't prevent an enter though
SuckAToad
SuckAToad3mo ago
well crap lol still a good read! deff more tools to add to my toolbox
Lisa
Lisa3mo ago
At that point it gets very hacky, try to detect when the first answer is too fast and ignore it? just spitballing really
SuckAToad
SuckAToad3mo ago
is there a way to set a bool istypewriteractive = true/false that would reflect false once the typewriter effect is over?
Angius
Angius3mo ago
Clear the answer after the text is displayed and ask again
SuckAToad
SuckAToad3mo ago
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?
Lisa
Lisa3mo ago
your code is synchronous, it's over when it exits the function
Angius
Angius3mo ago
var isWriting = false;

async Task Write(string msg)
{
isWriting = true;

foreach (var ch in msg)
{
Console.Write(ch);
await Task.Delay(100);
}
Console.Write('\n');

isWriting = false;
}
var isWriting = false;

async Task Write(string msg)
{
isWriting = true;

foreach (var ch in msg)
{
Console.Write(ch);
await Task.Delay(100);
}
Console.Write('\n');

isWriting = false;
}
This should work for asynchronous code Doubly so for synchronous
SuckAToad
SuckAToad3mo ago
let me look up what that means, ill be back! thanks again
Lisa
Lisa3mo ago
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
SuckAToad
SuckAToad3mo ago
is the function still being called while the effect is still happening?
mtreit
mtreit3mo ago
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.
SuckAToad
SuckAToad3mo ago
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
mtreit
mtreit3mo ago
You might look at using Spectre.Console if you want to do colorized text, etc.
mtreit
mtreit3mo ago
Spectre.Console - Welcome!
Spectre.Console is a .NET library that makes it easier to create beautiful console applications.
SuckAToad
SuckAToad3mo ago
I will add it to my list! thank you!!!!