Key

In my code im trying to get it to take only 1 input then it wont take another input until the key press ahs been released
No description
58 Replies
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
When space is pressed it will take 1 input then immediatly another tehn doesnt take other inputs.
arion
arion14mo ago
You don't set keypressed here
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
thats to avoid key taking in more hten 1 input if i removed that it would take in all inputs also i want to try get it down to the point i can remove the Thread.Sleep(50);
arion
arion14mo ago
Console.ReadKey is a blocking call, you don't need sleeps if you plan your while loops according to that.
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
if i dont have the sleep then the loop will constantly go through taking every input
arion
arion14mo ago
Taking every input? Console.ReadKey returns 1 key
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
yes but it instantly repeats the loop so console.readkey is constantly taking in a new input
arion
arion14mo ago
because your exit condition is not met
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
what exit condition ? theres multiple ifs and whiles
arion
arion14mo ago
main exit condition is the finished bool. You can exit the loops too via break;
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
finished is there to allow the game to end when teh user presses escape
arion
arion14mo ago
Then what prevents you from just doing something like this?
void GameLoop()
{
while (true)
{
var lastKey = Console.ReadKey(true).Key;

switch (lastKey)
{
case ConsoleKey.Escape:
return;
case ConsoleKey.Spacebar:
// spacebar logic
break;
case ConsoleKey.S:
// shop logic
break;
}
}
}
void GameLoop()
{
while (true)
{
var lastKey = Console.ReadKey(true).Key;

switch (lastKey)
{
case ConsoleKey.Escape:
return;
case ConsoleKey.Spacebar:
// spacebar logic
break;
case ConsoleKey.S:
// shop logic
break;
}
}
}
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
would that just constantly run as true next gets changed to false ?
arion
arion14mo ago
while (true) runs until the return keyword is triggered in the Method
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
but if i use the return in another procedure it wouldnt have an effect say i were in shop procedure and press escape as its a variable i can set it to false then it insta ends game
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
doesnt make a difference tho
arion
arion14mo ago
shop is a method. You would need to check for input inside the shop method for an escape prompt. You can use a method for that too then. eg.
public void EndGameScreen()
{
Console.Clear();
Console.WriteLine($"You ended with {ScoreManager.Coins}");
Environment.Exit(0);
}
public void EndGameScreen()
{
Console.Clear();
Console.WriteLine($"You ended with {ScoreManager.Coins}");
Environment.Exit(0);
}
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
what i have works the issue is happening within the while loop more specificly the if statement
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
^thats all the code
TheRanger
TheRanger14mo ago
can u use $paste instead
MODiX
MODiX14mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
i can try
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
BlazeBin
A tool for sharing your source code with the world!
MODiX
MODiX14mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
how do i use it ?
TheRanger
TheRanger14mo ago
paste ur code there and save it
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
BlazeBin - ygzzctyynomz
A tool for sharing your source code with the world!
MODiX
MODiX14mo ago
If your code is too long, you can post to https://paste.mod.gg/ and copy the link into chat for others to see your shared code!
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
that ?
TheRanger
TheRanger14mo ago
yea but no need to write $ paste
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
ah k
TheRanger
TheRanger14mo ago
whats ur issue again?
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
when the code runs and we go to the the procedure game(); when psace is pressed and held it adds 1 to coins the instantly goes through again then wont add again i want it to add once then it wont add until the player has released the space bar and presses it again
TheRanger
TheRanger14mo ago
ah i ran ur code i see what u mean
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
everything iv tried has either got it to register all inputs or no inputs
TheRanger
TheRanger14mo ago
did u try debugging? $debug
MODiX
MODiX14mo ago
Tutorial: Debug C# code - Visual Studio (Windows)
Learn features of the Visual Studio debugger and how to start the debugger, step through code, and inspect data in a C# application.
TheRanger
TheRanger14mo ago
im not a fan of how the console handles keyboard input
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
ye i have tried
TheRanger
TheRanger14mo ago
i dont think the console can detect if the key got released
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
not directly it can check if a key is available or not if its available it means its being pressed if its not then its not getting pressed
TheRanger
TheRanger14mo ago
well the issue here is since how windows handles the input if ur holding space it thinks ur constantly pressing space very fast when u hold down a button in notepad or discord for example it writes the first letter, then waits a few milliseconds, then constantly writes the letter
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
yeah but when you execute and hold the space it will add first twice then wont ever add again no matter how logn you hold it
TheRanger
TheRanger14mo ago
probably because of thread.sleep it wrote too fast the console had no time to check if a key isnt pressed if you removed thread.sleep it will constantly add a coin
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
if the time is reduce it will take an input while still ehld down its only at 50 where it wont take another input
TheRanger
TheRanger14mo ago
i dont know what u mean, that doesnt happen to me
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
change the time to 40 then press and hold change it to 1 press and hold keep at 50, press and hold
TheRanger
TheRanger14mo ago
in thread.Sleep?
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
all are dif outcomes ye
TheRanger
TheRanger14mo ago
still doesnt happen to me it depends on your keyboard settings probably
TheRanger
TheRanger14mo ago
you see this
No description
TheRanger
TheRanger14mo ago
that controls how fast it repeatedly writes the letter ur pressing
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
mines the same as whats in the images
TheRanger
TheRanger14mo ago
well mine is not that fast and im not on windows
Lucky_Pepsi_best
Lucky_Pepsi_bestOP14mo ago
i dont actually need a fix but id prefer to have 1 the current state is suitable ill try further but if i dont succeed its fine
Want results from more Discord servers?
Add your server