How to find parent function/class of generators?

Terry Reedy tjreedy at udel.edu
Sat Aug 25 14:17:42 EDT 2007


"J. A. Aczel" <seventhhs at gmail.com> wrote in message 
news:b410cefd0708250119p7b4080e0v186967c0ef60f720 at mail.gmail.com...
| Hello all. I think I should explain my problem in detail, so everyone
| understands what I'm trying to do and why. There may be a better
| approach to the whole problem then I am using.
|
| I am working on a game, with most of my AI code & such divided up into
| generator functions (really coroutines that don't take any input)
| stored in a dictionary called Director.tasks, and handled by a simple
| cooperative multitasking engine. The dictionary key is the generator
| object to be advanced, and the value is the frame number at which it
| should next be executed. The generator objects yield the delay before
| their next execution.
|
|    def realtick(self):
|        Director.frame += 1
|        for task in Director.tasks.items():
|            if task[1] <= Director.frame:
|                try:
|                    result = task[0].next()
|                    if result == -1:
|                        Director.tasks.pop(task[0])
|                    else:
|                        Director.tasks[task[0]] = Director.frame + result
|                except:
|                    Director.tasks.pop(task[0])
|
| Why did I use a dictionary? Originally I had a list of lists, but I
| thought converting it to a dictionary would make removing tasks from
| the queue easier, the main difficulty I'm having. But it didn't,
| because I can't identify the

The standard way of storing multiple tasks for future execution at a 
varietyu of times is a priority queue.  Python's heap class (module) is one 
way to implement such.

| Due to the framework I'm using, I think (or possibly just the Python
| language?) in-game objects aren't actually destroyed when they're
| removed from the game world. Thus, when an enemy or something with
| associated tasks 'dies', the tasks merrily continue executing... and
| the game will have a bunch of enemies who no longer have sprites,
| movement abilities or collision checks, but otherwise are none the
| worse for wear for having been destroyed. It's a big mess. So I need a
| way to remove all tasks associated with a game object from the queue,
| upon the object's  'destruction'.

I suspect you need to keep a list of tasks associated with a creature with 
each creature.  When creature dies, remove them from the queue or keep a 
set of dead tasks that tasks removed from the queue are checked against 
before being executed.

Good luck.

tjr








More information about the Python-list mailing list