Rene, Greg and Gumm, Thank you very much for your answers to my question.
Rene: Thank you for the comment of having "all objects able to accept events". That is a powerful idea that I was just starting to realize, but you made it very clear. In fact, I had started down the path by having some of my objects have a handle_vents method in them already. Gumm: Thank you for the links to your code. I've started looking at those, and I'll see what I can learn/borrow from them. Thanks, Irv > On Feb 10, 2017, at 5:30 PM, 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.