11-22-2022, 03:47 AM
We were also discussing this topic over at our Discord channel, and it seems there may be a few use cases where you'd actually place more code inside the While: Wend loop than I mentioned -- and that's basically whenever your main loop is running at an incredibly slow rate.
For example:
Try the above and see how many times you have to click the mouse before a click registers. (For me, it's about half a dozen times, give or take.) Your processing time is much less than your down time with the limit, and a vast majority of mouse buttons are lost when you clear the buffer. This isn't exactly an idea approach to handling things...
So, we can try something like the following:
Now here, we can click all we want, and every click event is read. The problem with this approach is that our program is running one loop per second, the user is entering multiple clicks per second -- we're going to generate lag in there, no matter what!!
If I can count one penny per second, and you suddenly hand me a dozen pennies, it's going to take me 12 seconds before I can do anything else besides count those pennies. < -- That's basically what we're seeing here.
So, we can take a middle ground approach:
Like this, we check for a mouseclick inside our mouseinput loop, and we basically set a flag if one occurs. It'll limit us to ONE click event per loop, no matter how many times the user mashes in the button, so we'll end up missing some clicks -- but we won't lag up the rest of our program either. We're running at one loop per second, and we're throttling clicks down to a maximum of one click per second.
For most people, and most programs (which you seldom ever see with less than a limit 30, 60, or even higher), this type of concern isn't one they'd ever have to worry over. Their program polls the mouse faster than a human can click a button up and down, so there's no chance for that backlog to appear in the buffer to the point where they start to miss events.
Once a program gets bogged down to the point where mouseclicks CAN be missed, however, there's really no "golden" solution for how to deal with things. Your three choices are basically:
1) Skip events so the program flows as smoothly as possible.
2) Deal with every event, even if it causes the program to have to pause execution until it's caught up and cleared the buffer.
3) Try to find a middle ground and watch to see if an event occured during the previous execution of the main program, and if so, deal with it -- even if you lose some other events in the process.
I mentioned that you never want anything inside that While - Wend _MOUSEINPUT Loop... That's not entirely true in extreme use cases, like the ones I showcased above.
One thing to remember: For every rule someone gives you in programming, there's always some exception to that rule -- no matter how rare, extreme, or unique it might be.
For example:
Code: (Select All)
oldmb = -1 'count as if the mouse is down to begin with
'this is so the mouse event has to have an UP then DOWN event to count as a click
Do
While _MouseInput: Wend
mb = _MouseButton(1)
If mb And Not oldmb Then
count = count + 1
Print "You just clicked the button! For a total of"; count
End If
oldmb = mb
_Limit 1
Loop Until _MouseButton(2) Or _KeyHit > 0
Try the above and see how many times you have to click the mouse before a click registers. (For me, it's about half a dozen times, give or take.) Your processing time is much less than your down time with the limit, and a vast majority of mouse buttons are lost when you clear the buffer. This isn't exactly an idea approach to handling things...
So, we can try something like the following:
Code: (Select All)
oldmb = -1
Do
While _MouseInput And Not click
mb = _MouseButton(1)
If mb And Not oldmb Then click = -1
oldmb = mb
Wend
If click Then
count = count + 1
Print "You just clicked the button! For a total of"; count
click = 0
End If
_Limit 1
Loop Until _KeyHit = 27
Now here, we can click all we want, and every click event is read. The problem with this approach is that our program is running one loop per second, the user is entering multiple clicks per second -- we're going to generate lag in there, no matter what!!
If I can count one penny per second, and you suddenly hand me a dozen pennies, it's going to take me 12 seconds before I can do anything else besides count those pennies. < -- That's basically what we're seeing here.
So, we can take a middle ground approach:
Code: (Select All)
oldmb = -1
Do
While _MouseInput
mb = _MouseButton(1)
If mb And Not oldmb Then click = -1
oldmb = mb
Wend
If click Then
count = count + 1
Print "You just clicked the button! For a total of"; count
click = 0
End If
_Limit 1
Loop Until _KeyHit = 27
Like this, we check for a mouseclick inside our mouseinput loop, and we basically set a flag if one occurs. It'll limit us to ONE click event per loop, no matter how many times the user mashes in the button, so we'll end up missing some clicks -- but we won't lag up the rest of our program either. We're running at one loop per second, and we're throttling clicks down to a maximum of one click per second.
For most people, and most programs (which you seldom ever see with less than a limit 30, 60, or even higher), this type of concern isn't one they'd ever have to worry over. Their program polls the mouse faster than a human can click a button up and down, so there's no chance for that backlog to appear in the buffer to the point where they start to miss events.
Once a program gets bogged down to the point where mouseclicks CAN be missed, however, there's really no "golden" solution for how to deal with things. Your three choices are basically:
1) Skip events so the program flows as smoothly as possible.
2) Deal with every event, even if it causes the program to have to pause execution until it's caught up and cleared the buffer.
3) Try to find a middle ground and watch to see if an event occured during the previous execution of the main program, and if so, deal with it -- even if you lose some other events in the process.
I mentioned that you never want anything inside that While - Wend _MOUSEINPUT Loop... That's not entirely true in extreme use cases, like the ones I showcased above.
One thing to remember: For every rule someone gives you in programming, there's always some exception to that rule -- no matter how rare, extreme, or unique it might be.