WaitEvent() cannot be called in functions triggered by some Event?

Discuss any general programming issues here
Post Reply
nexus
Posts: 150
Joined: Sun Mar 07, 2010 11:54 am

WaitEvent() cannot be called in functions triggered by some Event?

Post by nexus »

Hi,

I tried to replace Hollywood's`WaitLeftMouse` function by an own version.
Reason: When waiting with `WaitLeftMouse` the entire script execution is stopped, means no other events can be processed, e.g. timer event. I display a clock on screen that I want to be working continuously even when actively waiting for other events.

So, I thought, to be smart by just calling `WaitEvent()`, ignoring all events but `OnMouseButton` event.
It works, but only if not called in a function that was triggered by an event. See code below.

Code: Select all

@VERSION 8,0
@DISPLAY { Title = "Test", Width=800, Height=600, Mode="Windowed", Color = #BLACK }

EnableLayers()
EscapeQuit(True)


Function p_Dummy() EndFunction

Function p_WaitLeftMouse()
  DebugPrint("Waiting for MouseButton")
  InstallEventHandler({OnMouseDown = p_Dummy})
  While True
    Local info = WaitEvent()
    If info.action = "OnMouseDown" And info.triggered=1 Then Break 
  Wend
  DebugPrint("Got MouseButton")
  InstallEventHandler({OnMouseDown = 0})
EndFunction

Function p_WaitForMouseTest()
  p_WaitLeftMouse() 
EndFunction

Function p_Test()
  ;does not work:
  SetInterval(1,p_WaitForMouseTest,2000)
EndFunction

; works fine:
DebugPrint("Wait 1")
p_WaitLeftMouse()
DebugPrint("Wait 2")
p_WaitLeftMouse()
DebugPrint("Wait 3")
p_WaitLeftMouse()
DebugPrint("Wait 4")
p_WaitLeftMouse()

p_Test()

Repeat
	WaitEvent
Forever
Error Message is:
Wrong usage/parameters for this command! Read the documentation! File: test.hws (current line: 15 - in function: WaitEvent)
The error message seems not to be fitting :)
But I guess there is a good reason, why this is not possible?

Cheers,
nexus
User avatar
airsoftsoftwair
Posts: 5830
Joined: Fri Feb 12, 2010 2:33 pm
Location: Germany
Contact:

Re: WaitEvent() cannot be called in functions triggered by some Event?

Post by airsoftsoftwair »

nexus wrote: Mon May 20, 2024 1:42 pm The error message seems not to be fitting :)
But I guess there is a good reason, why this is not possible?
Indeed. On several systems calling WaitEvent() will enter the program main loop and will never return. The documentation is actually a bit of out of date here because it says that WaitEvent() will return information about the last event handler it executed. This is still true for some platforms but it's no longer guaranteed. When using RapaGUI on a non-Amiga system, for example, WaitEvent() will never return because it starts the main loop and this can only ever be started once which is also why it's forbidden to use WaitEvent() in callbacks. This is also in the manual of WaitEvent(), quote:
Note that WaitEvent() must not be called from any callbacks executed by WaitEvent(). In general, you should use WaitEvent() only once in your script: in your main loop. If you really have to check for events in a callback executed by WaitEvent(), use CheckEvents() instead, but this should normally be unnecessary.
nexus
Posts: 150
Joined: Sun Mar 07, 2010 11:54 am

Re: WaitEvent() cannot be called in functions triggered by some Event?

Post by nexus »

Thanks Andreas. Makes sense and too bad :D

So far, I have been using WaitEvent() here and there instead of Wait() to delay the program flow but still get and handle time interval events.
Usually, the return value of WaitEvent() is in such a case of no interest. I used the return value only for awaiting a specific event.

Have to think if using WaitEvent() more than once is still a good idea :D

And I should more carefully read the documentation. ;)

Cheers,
Tom
Post Reply