Wait for event in custom block?

Parent here, trying to help kid... We are trying to make a "timed ask" coming from the stage. In parallel, it launches (1) a custom block that displays and increments a countdown timer from the current (timer) sprite and (2) tells the stage to ask a question. Whichever one completes first is intended to stop the other. It works great, however we wanted to make it a reporter instead of setting timer_answer. We can't find a way (other than triggering outside event code) to make it wait for one (but not both) of the parallel events to complete. There must be a way to do this...

image

Any help would be much appreciated!

I'm not sure I understand what the scope of the upvar would be. Could you show code?

Hi! Welcome to Snap!.

I think probably the best solution will involve continuations. (See Section X of the Reference Manual.) Give me a while to experiment...

My apologies. I updated the code slightly in another thread, which you can feel free to delete, since you all have responded here. Here's the new code:

image

We've tried to use "wait until" (as you see here), but the "stops" are stopping the outer thread as well. Actually, we can even live without it being a reporter... however, it's just unusable as is because when this block has another block following, it doesn't wait for the earlier of the two events. It passes control immediately to the next block after it. This is basically an "ask or timeout" block, and we want it to wait until one of those occurs. If you could even just help us with the wait issue, that would be fantastic!

I'm also re-reading the continuations chapter of the manual. I have to admit, that while I'm familiar with certain aspect of functional programming (e.g., map, filter, reduce, function factories, python "with" blocks, etc.) I'm having difficulty applying the text in that chapter to this problem. I'll keep reading while you're experimenting. If I come up with anything, I'll post a further reply.

Many thanks... we know you all are busy.

I made a custom block in the past that fixes that ill try to find it again!

You can actually hide a sprite and it will have the same effect. You can tell a specific sprite that is hidden (preferably a sprite specifically for this block) to launch the ask block. Example:


Then, you can use the Wait block to wait out the time.
Block Image:

(P.S. you can right-click a script and click "script pic..." and take a screenshot of the code in high definition.)

This is very helpful. Didn't know the hidden sprite trick or the script pic trick!

If I have an actual sprite "countdown" graphic sequence in place of the wait (that's what my original timer block was), how do I factor that in? What if wait is 10 sec but "ask" is answered in three? How do I make the countdown stop and hide and the outer custom block to return control? I want the game to continue early and the countdown to disappear if it's answered early...

timer

To be clear, I don't want to change the timer block above. I only include it for reference. I want to try to encapsulate all the ask stuff (which is one user of, but not the only user of timer) into the customized ask block pictured at the top of this post. Thanks!

I believe this will solve your problem:


Make sure you use this block somewhere other than the hidden sprite.

untitled script pic (17)
Used this way
untitled script pic (18)

"true" means "timeout"

Or better yet, with the default value, so you always get a meaningful response without conditional processing
AskTout script pic

Thanks so much for this. I like this answer, but I just can't get it to work. The "stop other scripts in sprite" seems to be stopping the final two commands before they are executed. It never reports for me. Do you know why that would be?

I'm assuming the Q in "ask Q for ..." is just the name of the sprite containing the block, correct?

That's why joecooldoo wants you to use a separate sprite for the two processes, so the STOP block won't stop the main script. (Remember that LAUNCH starts a new thread running your launched script, so the script that calls LAUNCH is an "other script" relative to those launched threads.)

P.S. So far I haven't gotten it to work with continuations, which I think means there's a bug in our implementation of continuations. I'll look into it further...

I'm trying joecooldoo solution now. Will report back.

Would you mind posting the non-functional code for the continuation method? Pretty please? I know it won't work if there's a bug... I just want to see if I'm thinking along the right track. I've been reading and re-reading the continuations section in the manual to see if I could come up with anything.

It seems to me this should be a generalizable problem. I want to launch 2 (or n) independent threads a la "do in parallel", but I want to wait only until the FIRST returns, and then stop all remaining threads.
"Do in parallel and wait" wants to wait for all of them. I was thinking of trying to first solve this general problem with continuations and then try to apply it to the "ask with countdown animation" issue... but I will keep thinking.

Thanks so much for continuing to look into it. My kid is very excited that there are so many nice people (including folks on the development team) willing to lend a hand. What a great community!

The project is at
https://snap.berkeley.edu/snap/snap.html#present:Username=bh&ProjectName=ask%20and%20wait%20n%20secs
Here's what the code looks like:

P.S. The manual shows how to build a thread system out of continuations.

What exactly does it mean by "continuation"?

it continues a script...?

Formally there should be a wait(0) to allow, at least,one iteration of "wait until" & "report" before stopping scripts. But for me it works as expected as :snap: do some internal, non obvious, yielding.
AskTout script pic (1)

Maybe some setting in this projects do the trick
Username=dardoro&ProjectName=AskTout

This isn't a 25-words-or-less question. I mean, I can define it quickly: The continuation of a block that's part of a script is the work that remains to be done after that block finishes. But to understand the implications, you should read section X of the manual.


Does it really work for You?
"cont" is an empty ring so running it, when timeout, does nothing...
Also, how do you dismiss the "ask" prompt?

No, as I said, there seems to be a bug in call w/continuation -- that ring shouldn't be empty. But cinnapoca wanted to see it anyway.