Page 1 of 1

Problem with custom callbacks and WaitEvent (Wrong usage/parameters for this command!)

Posted: Thu Nov 08, 2018 12:03 pm
by Clyde
This is related to this problem I have: https://forums.hollywood-mal.com/viewto ... f=2&t=1997

Basically, after the text I fade in (and later fade out) asynchronly I want to fade in and out another text (like "ready", "set", "go"). As fade in and out are/will be async I don't know when the fade out process is ready. So I want to use a callback which calls the same fade in/out function again like 'p_showTextAsync("Ready") and then "p'showTextAsync("Set")' and so on.

I made a small example (without the parameters) which shows the problem: The first time p_showTextAsync() is called everything works fine, but when this function is called again via the callback, in the first loop I get this error when WaitEvent() is called: "Wrong usage/parameters for this command!"

I don't know why it works in the first call but not when called second!?

Here is the code:

Code: Select all

timeoutID = Nil

counter = 0

Function p_DummyForTextFading()
	; (unfortunately) needed for setInterval. Otherwise the frame printing loop would not continue
EndFunction

Function p_main()
	InstallEventHandler({OnKeyUp = p_EventFunc})
	
	SetInterval(2, p_DummyForTextFading, 1000/25) ; needed, so WaitEvent after AsyncDrawFrame() call continues
	
	Local myCallback = {["function"] = p_showTextAsync}
	
	p_showTextAsync(myCallback )

EndFunction

Function p_showTextAsync(callback)
	
	DebugPrint("counter: " .. counter)	
	
	For Local k = 1 To 20	
   	    	    
		;do async stuff like AsyncDrawFrame(textObject , k) ; draw frame by frame
		DebugPrint("k: " .. counter .. " - " .. k)
		
		; VWait or the like are not suitable to use in this case as it blocks the script so no (keypress) event can be handled while anim
		WaitEvent() ; needed, so the loop continues (through SetInterval) and listens to keypress events (InstallEventHandler)
		; throws error when p_showTextAsync() is called the second time
		
	Next
	
	timeoutID = SetTimeout(Nil, p_FadeTextOut, 1000, callback) ; begin to fade out the text after 5 seconds; TODO: make async
	
EndFunction

Function p_FadeTextOut(message)
	
	timeoutID = Nil
	
	counter = counter + 1
	
	If (HaveItem(message, "userData"))
		message.userData["function"]() ; execute callback after the fade out process has finished
	EndIf
	
EndFunction

p_main() ; app entry

While quit = False 
	WaitEvent 
Wend

Re: Problem with custom callbacks and WaitEvent (Wrong usage/parameters for this command!)

Posted: Thu Nov 08, 2018 10:00 pm
by airsoftsoftwair
It's not allowed to call WaitEvent() in a callback called by WaitEvent(). WaitEvent() should only be used once in your script to manage main loop event handling. Try using an interval function that calls AsyncDrawFrame() instead.

Re: Problem with custom callbacks and WaitEvent (Wrong usage/parameters for this command!)

Posted: Thu Nov 08, 2018 10:02 pm
by Clyde
Ok, I see (maybe that would be nice to mention in the WaitEvent() documentation?), thanks a lot!

Yeah, PEB showed a way how to use SetInterval(). I would have never come up with this idea (although it sounds quite logical now :-)).

Re: Problem with custom callbacks and WaitEvent (Wrong usage/parameters for this command!)

Posted: Sun Nov 11, 2018 10:18 pm
by airsoftsoftwair
Clyde wrote: Thu Nov 08, 2018 10:02 pm maybe that would be nice to mention in the WaitEvent() documentation?
Added :)

Re: Problem with custom callbacks and WaitEvent (Wrong usage/parameters for this command!)

Posted: Sun Nov 11, 2018 10:22 pm
by Clyde
Perfect! :-) Thanks!