C
C#โ€ข16mo ago
Mekasu0124

โœ… Creating a typewriter function that takes two optional arguments

public static void TypeWriter(List<string> input = null, string otherInput = null)
{
if (otherInput)
{
foreach (string line in input)
{
for (int i = 0; i < line.Length; i++)
{
Console.Write(line[i]);
Thread.Sleep(80);
}
}
}
else
{
for (int i = 0; i < otherInput.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
public static void TypeWriter(List<string> input = null, string otherInput = null)
{
if (otherInput)
{
foreach (string line in input)
{
for (int i = 0; i < line.Length; i++)
{
Console.Write(line[i]);
Thread.Sleep(80);
}
}
}
else
{
for (int i = 0; i < otherInput.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
I'm trying to write my own function that mimic the typewriter functionality that checks whether one or the other parameter has a value to use and if so, then execute the correct part of the if or else statement. I'm not understanding what I'm doing wrong here as it does not like if (otherInput) nor does it like if (otherInput.Equals(null)) so what am I supposed to put on that line, or how can I better check if the list is empty, then execute the else statement otherwise execute the if statement? Thanks
148 Replies
ero
eroโ€ข16mo ago
let me blow your mind
public static void TypeWriter(List<string> input) { }
public static void TypeWriter(string input) { }
public static void TypeWriter(List<string> input) { }
public static void TypeWriter(string input) { }
Mekasu0124
Mekasu0124OPโ€ข16mo ago
have two separate functions?
ero
eroโ€ข16mo ago
mhm
Mekasu0124
Mekasu0124OPโ€ข16mo ago
that's dope af. I didn't know you could have 2 functions with the same name O_o so when trying to build and run, it tells me that there is no overload function for the second typewriter?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
Mekasu0124
Mekasu0124OPโ€ข16mo ago
oh nvm. I'm dumb
Pobiega
Pobiegaโ€ข16mo ago
Do you want to be extra cool? Reduce code repetition by calling one overload from the other
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok so what if I wanted to have a function that if the spacebar for instance was pressed, it would skip and just display all the information at once? you have my interest ๐Ÿ˜›
ero
eroโ€ข16mo ago
didn't even notice the repitition
Pobiega
Pobiegaโ€ข16mo ago
Well the method that takes the list can just call the one that takes the string from inside the foreach
Mekasu0124
Mekasu0124OPโ€ข16mo ago
huh?
ero
eroโ€ข16mo ago
public static void TypeWriter(string input)
{
foreach (var c in input)
{
Console.Write(c);
Thread.Sleep(80);
}
}

public static void TypeWriter(List<string> input)
{
foreach (var s in input)
TypeWriter(s);
}
public static void TypeWriter(string input)
{
foreach (var c in input)
{
Console.Write(c);
Thread.Sleep(80);
}
}

public static void TypeWriter(List<string> input)
{
foreach (var s in input)
TypeWriter(s);
}
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ah much appreciated guys โค๏ธ ok so what about having a spacebar method that when the spacebar is clicked, it skips the typewriting animation that I've created and just displays all the text? no matter where it's at in the typewriter process
Pobiega
Pobiegaโ€ข16mo ago
doable, with a little trick the problem is that calling ReadLine or ReadKey etc blocks the thread until an input is given, so we can't do that in the loop... but we can check if an input is available before reading it!
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok...how? I'm relatively new to doing stuff like this lol I've been working with avalonia for a bit now xD
Pobiega
Pobiegaโ€ข16mo ago
there is a property called KeyAvailable on Console
Mekasu0124
Mekasu0124OPโ€ข16mo ago
and no I don't know how to do this in avalonia either ok so I would set the KeyAvailable to a variable?
Pobiega
Pobiegaโ€ข16mo ago
no.
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
...
}
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
...
}
however, what you do instead of the ... is the tricky bit
Mekasu0124
Mekasu0124OPโ€ข16mo ago
public static void Main()
{
List<string> inputStrings = new()
{
"Welcome To Your New Adventure!\n",
"To Get Started, Please Select An Adventure Below\n\n",
"A) Go On A Walk\n",
"B) Go For A Swim\n",
"C) Go For A Drive\n\n",
"Your Selection: "
};

TypeWriter(inputStrings);

string userInput = Console.ReadLine().ToLower();

StartAdventure(userInput);
}
public static void Main()
{
List<string> inputStrings = new()
{
"Welcome To Your New Adventure!\n",
"To Get Started, Please Select An Adventure Below\n\n",
"A) Go On A Walk\n",
"B) Go For A Swim\n",
"C) Go For A Drive\n\n",
"Your Selection: "
};

TypeWriter(inputStrings);

string userInput = Console.ReadLine().ToLower();

StartAdventure(userInput);
}
this is the function that I want to make skipable with the space bar. Are we able to use this for the example?
Pobiega
Pobiegaโ€ข16mo ago
isn't it actually the TypeWriter function you want to make skippable? thats the one that does the sleeping and the slow writing
Mekasu0124
Mekasu0124OPโ€ข16mo ago
oh lol yea my fault
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
public static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
public static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
these
Pobiega
Pobiegaโ€ข16mo ago
mhm so where would you put the if statement I gave you above?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
public static void TypeWriter(List<string> input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
}
else
{
foreach (string line in input)
{
TypeWriter(line);
}
}
}
public static void TypeWriter(List<string> input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
}
else
{
foreach (string line in input)
{
TypeWriter(line);
}
}
}
?
Pobiega
Pobiegaโ€ข16mo ago
see, that wouldn't work very well
Mekasu0124
Mekasu0124OPโ€ข16mo ago
oh
Pobiega
Pobiegaโ€ข16mo ago
you'd have to be pressing space at the exact entrypoint of that method, and it will never check again pressing space while its working would do nothing
Mekasu0124
Mekasu0124OPโ€ข16mo ago
so it would need to a be while loop?
Pobiega
Pobiegaโ€ข16mo ago
you already have a loop
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ohhhhhhh
Pobiega
Pobiegaโ€ข16mo ago
and also, you're in the wrong method
Mekasu0124
Mekasu0124OPโ€ข16mo ago
wait
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
public static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
}
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
public static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
}
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
like that?
Pobiega
Pobiegaโ€ข16mo ago
$tias
Pobiega
Pobiegaโ€ข16mo ago
it will not do what you expect ๐Ÿ™‚
Mekasu0124
Mekasu0124OPโ€ข16mo ago
then just say no and tell me what I did wrong lol
Pobiega
Pobiegaโ€ข16mo ago
no Observe what it does when you press space then tell me
ero
eroโ€ข16mo ago
This kind of logical and critical thinking and observing is crucial to be able to code better If we just tell you what's wrong, you learned nothing
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I actually learn through repetitional behavior so it kind of does because when it's correct the first time, I use it repetatively and learn it much better that way. However, back to the example. It doesn't work
Pobiega
Pobiegaโ€ข16mo ago
you modified the list overload somehow undo that change and try again
Mekasu0124
Mekasu0124OPโ€ข16mo ago
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
break;
}
else
{
TypeWriter(line);
}
}
}
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
break;
}
else
{
TypeWriter(line);
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
yeah undo that. you can't Console.Write(aList);
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok so what do I do then because if I iterate through the list and it runs this check every time, then it's going to require me to hit the spacebar on each iteration of the loop to skip it instead of a one time press of the spacebar to skip the whole thing so what am I missing?
Pobiega
Pobiegaโ€ข16mo ago
well lets fix that first
ero
eroโ€ข16mo ago
i mean it depends on the behavior you want
Pobiega
Pobiegaโ€ข16mo ago
remember, your method can be called two ways you need to support space-skipping in both
ero
eroโ€ข16mo ago
do you want the user to the able to complete each line individually (requiring them to maybe spam the space bar, which isn't uncommon at all for things like dialogue) or do you want a single press of the spacebar to make the entire text appear at once
Mekasu0124
Mekasu0124OPโ€ข16mo ago
... instead of a one time press of the spacebar to skip the whole thing...
Pobiega
Pobiegaโ€ข16mo ago
but its also valid to call TypeWriter with only a single string, and that should be skippable too
Mekasu0124
Mekasu0124OPโ€ข16mo ago
so when the program starts, it starts the typewriter effect on this list. If the spacebar is pressed, it needs to skip the typewriter effect and just print this list to the screen
Pobiega
Pobiegaโ€ข16mo ago
but what about TypeWriter("asdasdsadsadsadadsadsadsadsadasdasdsadsadasdsadasdsadsadsad");
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I don't want it to skip in single strings. Just this list
Pobiega
Pobiegaโ€ข16mo ago
should that not also be skippable?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
nope just the list at the beginning of the program
Pobiega
Pobiegaโ€ข16mo ago
that doesnt make sense to me, but its your code...
ero
eroโ€ข16mo ago
what they're asking is, do you really want the current string to finishing typewriting and only then for the skip to take effect? because that's what your code would currently do
Mekasu0124
Mekasu0124OPโ€ข16mo ago
doesn't matter if it's at the beginning of the string, in the middle, or 3 letters from the end. Spacebar = skip and display
ero
eroโ€ข16mo ago
so then the individual string also needs to be skipped
Mekasu0124
Mekasu0124OPโ€ข16mo ago
the entire iteration through the list needs to be skipped. like all of it, but only on the list in the main function, not other string inputs after it. Just that main list, and skip the entire iteration(s) of the strings within the list. Skip it all when the spacebar is pressed and display the list to the console so like
if (Spacebar_is_pressed)
{
foreach (string line in inputStrings)
{
Console.Write(line);
}
}
else
{
TypeWriter();
}
if (Spacebar_is_pressed)
{
foreach (string line in inputStrings)
{
Console.Write(line);
}
}
else
{
TypeWriter();
}
Pobiega
Pobiegaโ€ข16mo ago
So then these are actually two different methods You have SkippableTypeWriter and NonSkippableTypeWriter
Mekasu0124
Mekasu0124OPโ€ข16mo ago
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
public static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
this is the one that is being skipped.
Pobiega
Pobiegaโ€ข16mo ago
you don't seem to understand ero's point thou the slowing down of the printing isnt taking place in that method its happening in the other method
ero
eroโ€ข16mo ago
you're not understanding. when the TypeWriter method is currently running on a string from within that list, and you press the spacebar, that string will still have to finish you want a way to interrupt that
Pobiega
Pobiegaโ€ข16mo ago
so while its printing a line, its not even aware that its currently in a list
Mekasu0124
Mekasu0124OPโ€ข16mo ago
this is more complicated than I thought it would be....I just want to press the spacebar and it stop the typewriter no matter where it's at, and just display all the information in the list to the console...simple is as simple does. I wasn't expecting it to be this complicated ๐Ÿ˜‚
Pobiega
Pobiegaโ€ข16mo ago
So, given a single thread, your program can only do one thing at a time, yes? (for non-async code, I should add)
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I would presume so
Pobiega
Pobiegaโ€ข16mo ago
So when we enter the TypeWriter(List...) version, it then does the foreach, and calls TypeWriter(string) TypeWriter(string) isnt aware that its being called from inside a list
ero
eroโ€ข16mo ago
i'll say this is probbaly easier when we don't call the string overload
Mekasu0124
Mekasu0124OPโ€ข16mo ago
correct, the parent function does though
Pobiega
Pobiegaโ€ข16mo ago
but the parent function isnt running. its waiting. the parent function is waiting for the "child" function to return and only then will it run code again
Mekasu0124
Mekasu0124OPโ€ข16mo ago
lol not that it was a bad idea at all, I like how we wrote the two functions, but in my defense, I wanted a simple if/else check system with 2 parameters xD
Pobiega
Pobiegaโ€ข16mo ago
this method will spend 99.999% of its time inside the "child" function
Mekasu0124
Mekasu0124OPโ€ข16mo ago
correct so when the child is interrupted, the parent needs to be interrupted too
Pobiega
Pobiegaโ€ข16mo ago
yes. but you dont want the child to be interruptable in most cases.
Mekasu0124
Mekasu0124OPโ€ข16mo ago
at this point, if it's easier to just interrupt the child function altogether, then let's go that route
Pobiega
Pobiegaโ€ข16mo ago
so what we're getting at is that you should probably copypaste the code from inside TypeWriter(string) into the loop of your list having one call the other only works if you want the same behaviour
Mekasu0124
Mekasu0124OPโ€ข16mo ago
back to my original question. I have two parameters that both default to null. How can I write this check system so that it can determine which parameter it needs to execute the TypeWriter() functionality on and then how can I incorporate the spacebar skip functionality onto the if part of the statement
Pobiega
Pobiegaโ€ข16mo ago
don't. thats a terrible design
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I'll come back to this later today. It's after 5am my time and I'm exhausted. I'll be back later. Thanks for your help so far
Pobiega
Pobiegaโ€ข16mo ago
just have two overloads, where the list overload is skippable
Mekasu0124
Mekasu0124OPโ€ข16mo ago
over 100 messages at this point, and we're still trying to do that....
Pobiega
Pobiegaโ€ข16mo ago
well, not really the problem isnt the overload I have the code ready since a while ago, but you're not understanding And this isn't the "have code written for you" channel, its the help channel.
Mekasu0124
Mekasu0124OPโ€ข16mo ago
cool. well for the last 5 hours of the night before this help channel, I've been told I'm wrong on just about everything that I've said or done. No that doesn't have anything to do with ya'll, but yet it's still my fault because I'm not understanding so at this point, I just want to go to sleep. After I get off work later tonight, I'll come back to this with a better mindset.
Pobiega
Pobiegaโ€ข16mo ago
Sleeping is good. Coding while exhausted is a bad idea. Go sleep, come back refreshed and I'm sure you'll figure it out.
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I'm not going to figure it out on my own. I'd be more likely to just say nevermind. If it's a simple issue of my if statement for the spacebar is in the wrong spot, I don't understand why that can't just be said. I can understand trial and error to learn what I need to learn, but at this point I just feel like I'm going in circles and that's just going to cause me to be more defeated than anything else. I'm going to bed. good night
Pobiega
Pobiegaโ€ข16mo ago
If it's a simple issue of my if statement for the spacebar is in the wrong spot
It's not. Good night. Just bump this thread when you are back.
ero
eroโ€ข16mo ago
i think one of the better ways to handle it is with an out bool param
Pobiega
Pobiegaโ€ข16mo ago
out bool wasSkipped ish?
ero
eroโ€ข16mo ago
mhm
Pobiega
Pobiegaโ€ข16mo ago
I went with an enum result type on the line overload.. but since it shouldnbt be skippable, at this point I'm leaning towards them being separate
ero
eroโ€ข16mo ago
with that you can have 2 overloads as well. one where you care about skipping, and one where you don't
Pobiega
Pobiegaโ€ข16mo ago
true
Mekasu0124
Mekasu0124OPโ€ข16mo ago
Ok. I have a little bit before work. Whatโ€™s going to be the best way to go about doing this? Iโ€™ve given it some though, and itโ€™s not that important that only one portion is skippable so we can make it all skippable. With that being said, whatโ€™s going to be the better way to go about this?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
Also, it made more sense to move my typewriter functions to a Helpers.cs file so I moved them over
Pobiega
Pobiegaโ€ข16mo ago
so, the skipping needs to happen in the "child" function, because thats where you have a chance to respond to the spacebar click that method then needs to inform the "parent" function that a skip was requested
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
internal static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
internal static void TypeWriter(List<string> input)
{
foreach (string line in input)
{
TypeWriter(line);
}
}
internal static void TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
ok so what would be the best method of attack on that? in the parent function, have a bool for _isSkipped with a while loop?
Pobiega
Pobiegaโ€ข16mo ago
focus on the "child" method for now it needs to communicate something up the call-chain, what ways are there to do that?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
if/else statement returning true/false?
Pobiega
Pobiegaโ€ข16mo ago
okay there we go, we could return a value.
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static void TypeWriter(string input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
return false;
else
{
// allow typewriter effect
}
}
internal static void TypeWriter(string input)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
return false;
else
{
// allow typewriter effect
}
}
Pobiega
Pobiegaโ€ข16mo ago
close like we discussed yesterday, you need to run that spacebar-check inside the loop and to return a bool, the return type of the method needs to be... ? ๐Ÿ™‚
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static bool TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
return false;
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
internal static bool TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
return false;
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
good, almost there the compiler will complain about a thing right now thou and I'd probably change it to return true when a skip was requested instead of false
Mekasu0124
Mekasu0124OPโ€ข16mo ago
question
Pobiega
Pobiegaโ€ข16mo ago
Ask.
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static string TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
continue;
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
internal static string TypeWriter(string input)
{
for (int i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input);
continue;
else
{
Console.Write(input[i]);
Thread.Sleep(80);
}
}
}
what if I put it back to a string method, and instead of returning a value, I just had it print the entire line and then continue to the next line iteration and if the spacebar is pressed again (so create a counter with a count check) like count = 2, then that's when it will alert the parent function to skip as well??
Pobiega
Pobiegaโ€ข16mo ago
then the user would have to keep pressing space to skip each line at a time
Mekasu0124
Mekasu0124OPโ€ข16mo ago
I edited my message. my fault so like the spacebar pressed once per string will just skip and show each line individually but if pressed twice in the same iteration, it skips the entire animation
Pobiega
Pobiegaโ€ข16mo ago
so a single press would skip the current line, and two presses would skip the entire thing? sure thats doable
Mekasu0124
Mekasu0124OPโ€ข16mo ago
right
Pobiega
Pobiegaโ€ข16mo ago
but it still needs to return a bool
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok that's fine. so how would I do this?
Pobiega
Pobiegaโ€ข16mo ago
well your "child" function should actually just look the exact same the way you wrote it above will not work an iteration there is actually just a character, so it will have printed half the line, then all the line, then it will continue printing the line :p
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok so I'm a little lost then.
Pobiega
Pobiegaโ€ข16mo ago
I'll hand you the child function for now you're close
private static bool SkippableTypeWriter(string input)
{
for (var i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input[i..]);
return true;
}

Console.Write(input[i]);
Thread.Sleep(80);
}

return false;
}
private static bool SkippableTypeWriter(string input)
{
for (var i = 0; i < input.Length; i++)
{
if (Console.KeyAvailable && Console.ReadKey(true).Key == ConsoleKey.Spacebar)
{
Console.Write(input[i..]);
return true;
}

Console.Write(input[i]);
Thread.Sleep(80);
}

return false;
}
so when you press space, it prints out the remainder of the input, then returns true
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok so where would i incorporate knowing whether the spacebar was hit once during the string or twice?
Pobiega
Pobiegaโ€ข16mo ago
so heres the thing thats not doable when you press space, it will stop that method immediately you wont have time to press twice so the counting has to be in the "parent"
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok ok that makes sense
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line) == true)
{
spacebarCount++;
}
}
}
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line) == true)
{
spacebarCount++;
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
yup so how do we modify this to take skipping into account?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok let me edit that code block wrap it in a while-loop? oh wait no
Pobiega
Pobiegaโ€ข16mo ago
no you're getting ahead of yourself first we need to figure out when to increase spacebarCount
Mekasu0124
Mekasu0124OPโ€ข16mo ago
like that? i'll bring it down so it doesn't get lost in the messages
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;
}
}
}
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
yes, but I'd prefer if (SkippableTypeWriter(line)) the == true isnt helping and just bloats it okay, good
Mekasu0124
Mekasu0124OPโ€ข16mo ago
and then check the count amount right?
Pobiega
Pobiegaโ€ข16mo ago
yep
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;
}

if (spacebarCount >= 2)
{
// skip all animations
}
}
}
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;
}

if (spacebarCount >= 2)
{
// skip all animations
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
yep
Mekasu0124
Mekasu0124OPโ€ข16mo ago
ok so now what
Pobiega
Pobiegaโ€ข16mo ago
we can actually make this a tiny bit nicer thou
Mekasu0124
Mekasu0124OPโ€ข16mo ago
oh?
Pobiega
Pobiegaโ€ข16mo ago
spacebarCount only ever increases inside that first if so we can stick the second if inside it means we run slightly less code on most iterations
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;

if (spacebarCount >= 2)
{
// skip all animations
}
}
}
}
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;

if (spacebarCount >= 2)
{
// skip all animations
}
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
yup so, how do we... "skip all animations"?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
Console.Write instead of TypeWriter
Pobiega
Pobiegaโ€ข16mo ago
sure. But lets pretend this list has 20 entries in it, and skip was requested instantly do you want to make 20 calls to Console.Write?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;

if (spacebarCount >= 2)
{
Console.Write(line);
}
else
{
TypeWriter(line);
}
}
}
}
internal static void TypeWriter(List<string> input)
{
int spacebarCount = 0;

foreach (string line in input)
{
if (TypeWriter(line))
{
spacebarCount++;

if (spacebarCount >= 2)
{
Console.Write(line);
}
else
{
TypeWriter(line);
}
}
}
}
Pobiega
Pobiegaโ€ข16mo ago
or do you make ONE call to it?
Mekasu0124
Mekasu0124OPโ€ข16mo ago
one?
Pobiega
Pobiegaโ€ข16mo ago
yes! that one call is gonna look a bit yanky thou
public static void TypeWriter(List<string> input)
{
var skipCount = 0;

for (var index = 0; index < input.Count; index++)
{
var line = input[index];

if (SkippableTypeWriter(line))
{
skipCount++;

if (skipCount >= 2)
{
Console.Write(string.Join(null, input.ToArray(), index + 1, input.Count - (index + 1)));
return;
}
}
}
}
public static void TypeWriter(List<string> input)
{
var skipCount = 0;

for (var index = 0; index < input.Count; index++)
{
var line = input[index];

if (SkippableTypeWriter(line))
{
skipCount++;

if (skipCount >= 2)
{
Console.Write(string.Join(null, input.ToArray(), index + 1, input.Count - (index + 1)));
return;
}
}
}
}
Mekasu0124
Mekasu0124OPโ€ข16mo ago
oh wow lol blew my mind
Pobiega
Pobiegaโ€ข16mo ago
so, we have to use for instead of foreach so we know where we are we only call the typewriter once, in the if and if a skip was requested twice, we join the "rest" of the input list into a single big string, and write that
Mekasu0124
Mekasu0124OPโ€ข16mo ago
and return out of it so it doesn't iterate again
Pobiega
Pobiegaโ€ข16mo ago
ye
Mekasu0124
Mekasu0124OPโ€ข16mo ago
bet. tyvm!
Pobiega
Pobiegaโ€ข16mo ago
I didnt test it with your "skip twice to skip all" thing, but it should work also note that in the "skippabletypewriter" method, I used Console.Write(input[i..]); to "finish the line" some of the line was most likely already printed, so we dont wanna print that again
Mekasu0124
Mekasu0124OPโ€ข16mo ago
right, I figured that's what that was for and it works beautifully! Thank you so much โค๏ธ
Pobiega
Pobiegaโ€ข16mo ago
see, this time you understood much easier. being well rested helps ๐Ÿ™‚
Mekasu0124
Mekasu0124OPโ€ข16mo ago
that it does ๐Ÿ™‚
Want results from more Discord servers?
Add your server