Seriously. Pun intended. It's really been doing my head in. !@#$
So I found I need to defer some code while waiting for an event to happen. But because of the cooperative multitasking nature of ReaScripts I've found it hard to put in a simple delay line because it doesn't work that way. Like others I expected defer() to work instantly and put the code on hold. Only to find it doesn't work that way. I suppose it's called defer for a reason and not instant. ;-)
I've read through the mespotine guide (TIA) about five times and soon it will be ten times. I think I finally get it now. I was confused by it running the code to the end after the defer. Then all defers after that being isolated to the routine calling it. Now I think I can make sense of it. The entire block of code must be executed on each defer from where Reaper starts it. If coming from main() then it must come back to it. If restarted from a defer location then the block is isolated to that routine so when that routine ends the script effectively ends until the next iteration. If I have understood it correctly.
Now here is where I am stuck. My need is for normal program flow. Setting up in main(). Calling a function that needs to wait for an event and return a value. Then coming back and continuing on in main(). The problem is a defer is needed to wait. But it disrupts the program flow. Effectively pulling the rug out. The waiting function would open some things it needs before it waits. But they could fail. So it needs to return success or fail. The problem here is that a defer is needed for the waiting. But the routine must return back immediately regardless. Because the defer needs to occur after the whole code block executes. After that routines exits. And this is where I'm stuck.
See, a top function deferring doesn't know where it was called from. Nor do I see any way for it to return. It would be handy here if there was a pause() function that suspended the script instead but there isn't. FR maybe? Yes I tried sleep() knowing full well what the consequences were. Lol. My main issue then is that I cannot stack the return address. And yes I am now thinking at the ASM level. I need to stack where my routine is called from so I can return back it. Either a defer or direct function call. But I can't do that either AFAIK.
I'm totally stuck on how to proceed. I have a simple flowing need in my program that's standard across the board. But I just can't see a way to solve it without jumping through hoops. And adding flags or checking functions. Because main() will be looped through before any waiting can even begin. I can only think that it would need to be coded in such a way as being aware that main() will be looped over and over and a simple function returning a result is not possible. Because such a function cannot be encapsulated in this manner. So would need to be divided into a set of open, check and close functions.
How would you guys solve a simple problem that is complicated by a deferring?
For reference the mespotine guide:
https://forum.cockos.com/showpost.ph...2&postcount=21
Long thread with exactly what I'm facing:
https://forum.cockos.com/showthread....ighlight=defer