Welcome!
This is the community forum for my apps Pythonista and Editorial.
For individual support questions, you can also send an email. If you have a very short question or just want to say hello — I'm @olemoritz on Twitter.
My second brick wall...
-
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.
-
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 thescene
-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
andthreading.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 athreading.Lock()
to ensure the action is only executed once in paralell.
If they are not seperate threads, you could use aqueue.Queue
or a simplelist
, append the call-specificargs
andkwargs
, 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 ownthreading.Thread
with aqueue.Queue
for queuing calls. - have a attribute/global
-
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.