Forum Archive

My second brick wall...

adrius42

Timing and Queuing

Either my code ends a game and opens a Game Over menu before an Action sequence was completed.eg pearly removing the last cards from the screen.

—or—

Or fast user interactions are so fast that calls to an object ( that removes pairs of cards for example) are too frequent and cause errors, that are not seen when user acts more slowly.

I am confident there are techniques that I need to find in the documentation... pointers to the right pages please, for

1 Method for allowing an action to complete, before Game Over

2 Method for queuing calls to a block of code

Thanks in anticipation

Part of my problem is 1 I don’t know the keywords to search for
And 2 the current document search is pretty unforgiving.

I often find myself backing out to Google to try and explore a Python concept.

bennr01

Take a look at threading.Event, which can be used to pause a thread until a flag is set.
I dont know if this works with the scene-module, but it may be what you are looking for.
Theoretically, you could do something like the following:

For 1:
- have a attribute/global myevent
- when the action is started, call myevent.set()
- when the is completed, call myevetn.clear()
- when the game-over condition is met, call myevent.wait() before showing the screen.

This should allow you to wait for one action to complete. For more than one action in parallel, take a look at threading.Semaphore and threading.BoundedSemaphore.

For 2:
This is a bit more complicated, but do you realy need to queue those calls?
If every call is in a seperate thread, you could use a threading.Lock() to ensure the action is only executed once in paralell.
If they are not seperate threads, you could use a queue.Queue or a simple list, append the call-specific args and kwargs, then have a loop taking the first element and processing the call.

I hope this helps you a bit :)

Edit: As @JonB pointed out scene does not use individual threads, thus this answer is mostly invalid. You could still create your own threading.Thread with a queue.Queue for queuing calls.

JonB

http://omz-software.com/pythonista/docs/ios/scene.html#scene.Node.remove_all_actions
will stop pending actions on a node. I wouldnt try to use any threading methods in scene; for one thing, I think scenes are all a single thread -- an update method gets called once per frame, and actions execute one cycle per frame. You dont want to block in any scene methods. Actions just get executed a step at a time before the next update is called.