while: wend
#17
(01-03-2023, 04:07 PM)james2464 Wrote: "ELI5" attempt:


I was confused by this as well.   WHILE _MOUSEINPUT: WEND looked like a single line of code to me.    The : symbol is used to separate lines of code on the same line. 

An example would be...

FOR t = 1 TO 10 : NEXT t


If you forget that and just think of it as two lines...

WHILE _MOUSEINPUT
WEND

Then you can just consider reply #2.   But that was also confusing to me a while ago.   I wondered how this doesn't just loop infinitely.
But it just goes and checks for the latest mouse input and then exits the loop.

It's a little more than just checking for the latest mouse input; it's clearing the mouse buffer itself and updating the mouse commands to the last input recieved.

For example, let's look at something that's easy to visualize and understand, that behaves in exactly the same way.   First, try the code below:

Code: (Select All)
Print "Press a few keys in the next 5 seconds."
_Delay 5
Print "You can stop pressing keys now."
_Delay 1
Print "The keys you pressed while I was paused were:"
Do
    k = _KeyHit
    Print k
Loop Until k = 0

Now, as you can see, even when the program is paused, it still records whatever keys you pressed and saved them in a buffer for you.  The DO..LOOP here runs in a quick loop until it clears out that buffer by reading it once character at a time and printing it to the screen for you.

But what if you didn't want to actually print to the screen?  What if you just wanted to clear that buffer, to reset it for some fresh input.  After all, you might not need to know what the user pressed while the program was paused.

While _KeyHit: Wend

^The above is all you'd need.  It'd read the keyhit buffer, clear it one character at a time, until there wasn't any characters left in it.

And that's basically what While _MouseInput: Wend does.  It reads the mouse buffer, one event at a time, and clears it until there's nothing left in that buffer.  When you exit that loop, _MouseX, _MouseY, and _MouseButton(#) are all whatever the last pass of the mousebuffer had in it.  You're just clearing out all the events which you don't need to process, and dealing with whatever the current event is.

It's that simple.  Wink
Reply


Messages In This Thread
while: wend - by fistfullofnails - 01-03-2023, 06:09 AM
RE: while: wend - by SMcNeill - 01-03-2023, 06:28 AM
RE: while: wend - by mnrvovrfc - 01-03-2023, 07:56 AM
RE: while: wend - by bplus - 01-03-2023, 03:39 PM
RE: while: wend - by mnrvovrfc - 01-03-2023, 07:54 PM
RE: while: wend - by bplus - 01-03-2023, 03:57 PM
RE: while: wend - by james2464 - 01-03-2023, 04:07 PM
RE: while: wend - by fistfullofnails - 01-05-2023, 07:36 AM
RE: while: wend - by SMcNeill - 01-05-2023, 08:38 AM
RE: while: wend - by Kernelpanic - 01-03-2023, 08:40 PM
RE: while: wend - by TempodiBasic - 01-04-2023, 12:13 AM
RE: while: wend - by bplus - 01-04-2023, 01:09 AM
RE: while: wend - by bplus - 01-04-2023, 01:55 AM
RE: while: wend - by SMcNeill - 01-04-2023, 02:52 AM
RE: while: wend - by bplus - 01-04-2023, 04:12 AM
RE: while: wend - by mnrvovrfc - 01-04-2023, 04:51 AM
RE: while: wend - by bplus - 01-04-2023, 05:22 PM
RE: while: wend - by Kernelpanic - 01-05-2023, 12:26 PM
RE: while: wend - by TerryRitchie - 01-05-2023, 04:55 PM
RE: while: wend - by TempodiBasic - 01-05-2023, 06:36 PM
RE: while: wend - by TerryRitchie - 01-05-2023, 11:04 PM
RE: while: wend - by fistfullofnails - 01-07-2023, 05:12 AM
RE: while: wend - by SMcNeill - 01-07-2023, 06:51 AM
RE: while: wend - by mnrvovrfc - 01-07-2023, 06:29 AM
RE: while: wend - by OldMoses - 01-07-2023, 01:55 PM



Users browsing this thread: 3 Guest(s)