It doesn't seem to be that good example, please don't do it like this in your code
This would be better to do with real events rather than a busy loop. Or if you do it with a busy loop like this example, add VWait() at the end of the main loop (before the Forever) to avoid it running full CPU load all the time.
I did test the program example and it is working as intended.
There's a bit of luck that it runs as intended
First two lines in the main loop:
Code: Select all
at this point both these are set to false false, so far so good.
Actually.. in my opinion these aren't set to "false false", but laststate is set to 1 if currentstate equals 0. So laststate can be 1 or 0 depending if currentstate is 0 or not. Currentstate won't be changed at all here and it's left what it was.
So, this line doesn't actually do anything, and you can remove it and it doesn't affect how the program works.
Next the code within the funtion is executed:
Code: Select all
And here I must be missing something. At first look that first line looks redundant, since both the variables are still false. The IsLeftMouse command isnt called yet, so it looks like laststate still must be false.
But clearly it isnt, because else the program would not work.
As currentstate wasn't touched at the previous step, laststate gets the correct value here and gets overridden by the previous 1 or 0, so this proves that the line in the mainloop is useless.
I was thinking that since IsLeftMouse is listed in the eventlibrary, it sort of would be monitored even when the program is elswhere in the loop. But if that was the case, why would Isleftmouse to be in the loop at all, could it not just be called once at program start?
IsLeftMouse reports the current value of the mouse button state when its line is executed. You should install a proper event handler to do this kind of things in nicer way and to get presses buffered. And use less global variables