โ Creating a typewriter function that takes two optional arguments
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? Thanks148 Replies
let me blow your mind
have two separate functions?
mhm
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?
oh nvm. I'm dumb
Do you want to be extra cool?
Reduce code repetition by calling one overload from the other
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 ๐
didn't even notice the repitition
Well the method that takes the list can just call the one that takes the string from inside the foreach
huh?
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
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!ok...how?
I'm relatively new to doing stuff like this lol I've been working with avalonia for a bit now xD
there is a property called
KeyAvailable
on Console
and no I don't know how to do this in avalonia either
ok so I would set the KeyAvailable to a variable?
no.
however, what you do instead of the
...
is the tricky bitthis is the function that I want to make skipable with the space bar. Are we able to use this for the example?
isn't it actually the TypeWriter function you want to make skippable?
thats the one that does the sleeping and the slow writing
oh lol yea
my fault
these
mhm
so where would you put the if statement I gave you above?
?
see, that wouldn't work very well
oh
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
so it would need to a be while loop?
you already have a loop
ohhhhhhh
and also, you're in the wrong method
wait
like that?
$tias
it will not do what you expect ๐
then just say no and tell me what I did wrong lol
no
Observe what it does when you press space
then tell me
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
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
you modified the list overload somehow
undo that change and try again
yeah undo that.
you can't
Console.Write(aList);
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?
well lets fix that first
i mean it depends on the behavior you want
remember, your method can be called two ways
you need to support space-skipping in both
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
... instead of a one time press of the spacebar to skip the whole thing...
but its also valid to call TypeWriter with only a single string, and that should be skippable too
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
but what about
TypeWriter("asdasdsadsadsadadsadsadsadsadasdasdsadsadasdsadasdsadsadsad");
I don't want it to skip in single strings. Just this list
should that not also be skippable?
nope
just the list at the beginning of the program
that doesnt make sense to me, but its your code...
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
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
so then the individual string also needs to be skipped
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
So then these are actually two different methods
You have
SkippableTypeWriter
and NonSkippableTypeWriter
this is the one that is being skipped.
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
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
so while its printing a line, its not even aware that its currently in a list
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 ๐
So, given a single thread, your program can only do one thing at a time, yes?
(for non-async code, I should add)
I would presume so
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 listi'll say this is probbaly easier when we don't call the string overload
correct, the parent function does though
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
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
this method will spend 99.999% of its time inside the "child" function
correct so when the child is interrupted, the parent needs to be interrupted too
yes. but you dont want the child to be interruptable in most cases.
at this point, if it's easier to just interrupt the child function altogether, then let's go that route
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 behaviourback 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
don't.
thats a terrible design
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
just have two overloads, where the list overload is skippable
over 100 messages at this point, and we're still trying to do that....
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.
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.
Sleeping is good. Coding while exhausted is a bad idea.
Go sleep, come back refreshed and I'm sure you'll figure it out.
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
If it's a simple issue of my if statement for the spacebar is in the wrong spotIt's not. Good night. Just bump this thread when you are back.
i think one of the better ways to handle it is with an
out bool
paramout bool wasSkipped
ish?mhm
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
with that you can have 2 overloads as well. one where you care about skipping, and one where you don't
true
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?
Also, it made more sense to move my typewriter functions to a Helpers.cs file so I moved them over
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
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?
focus on the "child" method for now
it needs to communicate something up the call-chain, what ways are there to do that?
if/else statement returning true/false?
okay there we go, we could return a value.
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... ? ๐
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
question
Ask.
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??
then the user would have to keep pressing space to skip each line at a time
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
so a single press would skip the current line, and two presses would skip the entire thing?
sure
thats doable
right
but it still needs to return a bool
ok that's fine. so how would I do this?
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
ok so I'm a little lost then.
I'll hand you the child function for now
you're close
so when you press space, it prints out the remainder of the input, then returns true
ok so where would i incorporate knowing whether the spacebar was hit once during the string or twice?
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"
ok
ok that makes sense
yup
so how do we modify this to take skipping into account?
ok let me edit that code block
wrap it in a while-loop?
oh wait no
no
you're getting ahead of yourself
first we need to figure out when to increase
spacebarCount
like that?
i'll bring it down so it doesn't get lost in the messages
yes, but I'd prefer
if (SkippableTypeWriter(line))
the == true
isnt helping and just bloats it
okay, goodand then check the count amount
right?
yep
yep
ok so now what
we can actually make this a tiny bit nicer thou
oh?
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
yup
so, how do we... "skip all animations"?
Console.Write instead of TypeWriter
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
?or do you make ONE call to it?
one?
yes!
that one call is gonna look a bit yanky thou
oh wow lol blew my mind
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 thatand return out of it so it doesn't iterate again
ye
bet. tyvm!
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 againright, I figured that's what that was for
and it works beautifully! Thank you so much โค๏ธ
see, this time you understood much easier. being well rested helps ๐
that it does ๐