Hey ya,

so... a few ways.

# First threading. It's not so quick with threads, and delay can be longer
than you hope.
# https://docs.python.org/3/library/threading.html#threading.Timer
from threading import Timer
def hello():
    print("hello, world")
seconds = 1.0
Timer(seconds, hello).start()


# secondly, add event handling to your scene graph.
I usually like to have all my objects able to accept events. But as you
say, it takes a bit of work to wire it through to everything. I make all my
objects have a similar signature (like sprites for example), and then add a
handle_events to all of them.


# thirdly,  You can use the event queue, register callbacks anywhere, but
process callbacks in your main loop.

# store a list of callbacks somewhere.
mainmodule.callbacks = []

# then add event processors anywhere in your app.
def process_events(events):
    print('deep inside a nest of functions')
mainmodule.callbacks.append(process_event)

# in your main event loop.
for cb in mainmodule.callbacks:
    cb(events)

# when your code doesn't want the event handler any more.
mainmodule.callbacks.remove(process_event)


ps. You should totally try python3! It's sooooo much more elegant :)




On Sat, Feb 11, 2017 at 2:30 AM, Irv Kalb <i...@furrypants.com> wrote:

> I've been working on porting a game that I wrote in a different
> language/environment, and I want to make the Python/PyGame code as
> efficiently as possible.
>
> I have code in an object, that triggers an event that should occur, say 1
> second later.  I used pygame.set_timer, created an ID and set 1000
> milliseconds.  However, in order to catch that event, I need to have code
> in my main loop, which passes down knowledge of that event (potentially
> through several layers of objects), all the way back down to my object that
> created the timer.  I have this working, but it doesn't feel like a very
> clean implementation.
>
> As another very simple example, imagine that I wanted to make a generic
> animation object that would run an animation off of a list of images at
> some given speed (independent of the frame rate).  Ideally, I want to have
> a timer that tells the current instance of the animation object to change
> to the next image - directly.   It seems like this type of thing should be
> easy within a single class, but in practice, as I said, I have had to add
> code to several layers to get notification back to animation object(s)
> where I really want to get notification.
>
> So, my specific question is this:  Is there any way for an object to
> create a timer, that results to a callback within the same object?
>
> If not, I guess one way to do this would be to have a central Timer
> Manager object that I could call to set up a timer, and pass it a timer ID,
> amount of time to wait, (just like a call to set_timer), but also pass in a
> reference to the current object (self), and a method name to call back
> (maybe "animate").  Then, in the main loop, I could call the Timer Manager
> with all events, it would check if the event ID matched any it should be
> looking for, and if so, call the appropriate method of the given object.
> I'm sure I could make it work, but I'm wondering if I am missing something
> simple.
>
> Thanks in advance,
>
> Irv
>
> PS:  Still in Python 2, but I am seriously looking into potentially
> porting code to Python 3.  In fact, I had a talk with my manager at one of
> my colleges today about it.

Reply via email to