event and program flow

Find quick help here to get you started with Hollywood
Post Reply
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

event and program flow

Post by amyren »

I was reading the example code in this post,
viewtopic.php?f=4&t=2099#p11046

I did test the program example and it is working as intended.
But there is probably something about event handling that I did not get yet, since I struggle to understand this.

First two lines in the main loop:

Code: Select all

laststate=currentstate=0
p_Update_input() 
at this point both these are set to false false, so far so good. Next the code within the funtion is executed:

Code: Select all

	laststate=currentstate
	currentstate=IsLeftMouse()
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.

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?
User avatar
jPV
Posts: 604
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: event and program flow

Post by jPV »

amyren wrote: Wed Jul 31, 2019 10:29 pm I was reading the example code in this post,
viewtopic.php?f=4&t=2099#p11046
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

laststate=currentstate=0
p_Update_input() 
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

	laststate=currentstate
	currentstate=IsLeftMouse()
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 ;)
amyren
Posts: 361
Joined: Thu May 02, 2019 11:53 am

Re: event and program flow

Post by amyren »

Thank you, that clearifies it.

I was thinking that "laststate=currentstate=0" did set both to 0, but basicly it is the same as

Code: Select all

if currentstate=0 
	laststate=1
else 
	laststate=0
endif
I agree that they are technically not set to true or false here, but I did use that term in my post because later in that script these variables are checked for true/false state.
Post Reply