Possibly a bug with OnMouseUp on buttons

Report any Hollywood bugs here
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Possibly a bug with OnMouseUp on buttons

Post by Bugala »

Problem is that OnMouseUp seems to get activated when it shouldnt.

Here is example code:

Code: Select all

@DISPLAY {Width=1920, Height=1080, ScaleMode = #SCALEMODE_AUTO, FitScale = True}
EscapeQuit(True)

Function ClickToContinue(timetowait)
waitformousebuttonstogoup()

Local skip = False
While skip = False
   If IsLeftMouse()= True Or IsRightMouse() = True Then skip = True
Wend

EndFunction



Function waitformousebuttonstogoup()
Local skip=False
While skip=False
	VWait()
   If IsLeftMouse() = False And IsRightMouse() = False Then skip=True
Wend
EndFunction



Function startgame()
	DebugPrint("button active")
	End()
EndFunction


Function p_Init_TitleScreen()
Cls()
waitformousebuttonstogoup()
TextOut(100, 100, "mouse buttons are up now")
MakeButton(1, #SIMPLEBUTTON, 1 , 1, 1920, 1080, {OnMouseUp = Function() StartGame() EndFunction})  
EndFunction


Cls(#BLACK)
TextOut(#CENTER, 100, "<CLICK MOUSE TO CONTINUE>")
ClickToContinue(0)
Cls(#BLACK)

p_init_titlescreen()

Repeat
WaitEvent()
Forever
First there comes a text where you are supposed to click a mouse button to continue. After you press a mouse button, it will first check that both mouse buttons are up (=false). Only after this it makes this one screen sized button, and only then it goes to "WaitEvent" loop.

However, at least on my windows 10 machine, when i click, it activates this button right away.

If i click with right mouse button, it works fine (since left mouse button havent been pressed), but clicking left mouse button, and despite there being code checking that this mouse button is released before button is made and waitevent happens, it seems to still activate this OnMouseUp event.

You can also check by holding left mouse button down to see that it doesnt move forward until button is actually released, at which point you can see for a moment that new text appearing to screen, to see that it is not moving forward until button is released.
User avatar
jPV
Posts: 603
Joined: Sat Mar 26, 2016 10:44 am
Location: RNO
Contact:

Re: Possibly a bug with OnMouseUp on buttons

Post by jPV »

I don't think it's a bug, but maybe this is what happens: Hollywood caches input events for 60 seconds (by default), and when your program goes to p_Init_TitleScreen() function, these input events aren't handled before you set the new button. IsLeftMouse() and IsRightMouse() don't clear the event queue, but you get a new click in the queue there. The events are handled only when you return to WaitEvent() loop, and then you have both a mouse press in the event queue and the button there, so it takes that you pressed the button.

To avoid this you could add CheckEvents() to your p_Init_TitleScreen() function to clear the input event queue:

Code: Select all

Function p_Init_TitleScreen()
Cls()
waitformousebuttonstogoup()
TextOut(100, 100, "mouse buttons are up now")
CheckEvents()
MakeButton(1, #SIMPLEBUTTON, 1 , 1, 1920, 1080, {OnMouseUp = Function() StartGame() EndFunction})  
EndFunction
But in any case, I find it a bit bad coding practise to handle these kind of input events by these kind of While-Wend loops. I think you should do everything by event handlers. Or at least remember to add the VWait() in all loops, now the first one makes useless CPU load ;)

Or do you have to check the right mouse button here? It would be much simplier, for example, like this:

Code: Select all

@DISPLAY {Width=1920, Height=1080, ScaleMode = #SCALEMODE_AUTO, FitScale = True}
EscapeQuit(True)

Function startgame()
	DebugPrint("button active")
	End()
EndFunction

Function p_Init_TitleScreen()
    Cls()
    TextOut(100, 100, "mouse buttons are up now")
    MakeButton(1, #SIMPLEBUTTON, 1 , 1, 1920, 1080, {OnMouseUp = Function() StartGame() EndFunction})
EndFunction

Cls(#BLACK)
TextOut(#CENTER, 100, "<CLICK MOUSE TO CONTINUE>")
WaitLeftMouse()
Cls(#BLACK)

p_Init_TitleScreen()

Repeat
    WaitEvent()
Forever
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Possibly a bug with OnMouseUp on buttons

Post by Bugala »

I mostly copy-pasted that click thing from elsewhere in my code and did some alterations, hence the right mouse button was there too, plus it was good for checking that the event doesn't happen for some other error in code.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Possibly a bug with OnMouseUp on buttons

Post by airsoftsoftwair »

jPV wrote: Thu Aug 08, 2019 11:44 am But in any case, I find it a bit bad coding practise to handle these kind of input events by these kind of While-Wend loops. I think you should do everything by event handlers.
+1

That code smells like spaghetti :)
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Possibly a bug with OnMouseUp on buttons

Post by Bugala »

@airsoftsoftwair

but main question is if this is supposed behavior or not? For if it is, then I need to use something else than OnMouseUp.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Possibly a bug with OnMouseUp on buttons

Post by airsoftsoftwair »

I think it's as jPV analyzed: Those mouse events are still in the queue and then they trigger. In general, you shouldn't mix polling functions like IsLeftMouse() and event handlers. Either do everything using polling functions or do everything using event handlers but don't mix them because that could lead to several interference issues since event handlers are asynchronous and polling functions are synchronous.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Possibly a bug with OnMouseUp on buttons

Post by Bugala »

okay, thanks. WIll look at alternative solution to this then, for this comes up more than once to me.

As example I can have this intro animation playing that either goes automatically forward after waiting certain time, or then you can click mouse button to go forward. When intro ends, main menu loads.

So if player decides to skip the intro using left mouse, then i encounter this onmouseup problem.

I guess it could be changed into working with event handler only, just not sure how sensible that is, for basically i guess it would be to have intro played using setinterval and then having whole screen sized button to use for "clicktocontinue". Have to think that a bit how that would work.

Meanwhile, is there a possibility to add a command that would clear all events? That way i could first clear all events and then set up the button and do waitevent.
User avatar
airsoftsoftwair
Posts: 5443
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: Possibly a bug with OnMouseUp on buttons

Post by airsoftsoftwair »

There is an undocumented function named FreeEventCache() which you can try :)
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Possibly a bug with OnMouseUp on buttons

Post by Bugala »

ah, thanks. That did the trick.

Now I can continue with my bad coding habit.
Bugala
Posts: 1178
Joined: Sun Feb 14, 2010 7:11 pm

Re: Possibly a bug with OnMouseUp on buttons

Post by Bugala »

hmm. Actually.

Since WaitLeftMouse() is part of good coding habit, would it be possible to either extend this a bit or make altogether another command?

For I have made this "ClickToContinue"-function, but how the actual version that I keep using works is following:

It is used with "ClickToContinue(timetowait)"

Idea is that I for example put 5000 there. What now happens is that either user clicks either left or right mouse button, or after 5 seconds, it continues forward automatically. (using 0 means to wait until click happens)

Left and Right mouse buttons are both checked, since they have different functions.

Left mouse will skip to next text for example, while right mouse button will skip all the text.


So basically what I would need is "whichbuttonwaspressed = WaitMouseButton(timetowait)"

That way I could do exactly same as my current function is doing, but through event system.

But would this be doable, or does it not fit to the context of what kind commands should be?
Post Reply