call function of class instance with no assigned name?

George Oliver georgeoliverGO at gmail.com
Tue May 5 15:17:23 EDT 2009


On May 5, 11:59 am, Dave Angel <da... at ieee.org> wrote:

> 1) forget about getattr() unless you have hundreds of methods in your
> map.  The real question is why you need two maps. What good is the
> "command string" doing you?   Why not just map the keyvalues directly
> into function objects?


Thanks for the reply Dave. I understand your example and it's what I
originally used. Here is more detail on what I'm doing now, I hope
this will explain my question better.

In the game I'm writing the player, monsters, items and so on are
instances of class Thing, like:

class Thing(object):
    def __init__(self, x, y, name):
        self.x, self.y = x, y
        self.name = name
        self.brain = None

Some Things have an instance of class Brain attached. The Brain
instance has a list of handlers, like:

class Brain(object):
    def __init__(self):
        self.handlers = []

A handler class defines some functionality for the Brain. Each Brain
has an update method like this:

def update(self, arguments):
    for handler in self.handlers:
        handler.update(arguments)

So on each pass through the main game loop, it calls the update method
of all the brains in the game, which run their handler update methods
to change the state of the game.

A handler would be something like a key input handler. The key input
handler is defined separately from the command handler in the case I
want to use a different method of input, for example a mouse or
joystick.

In the dictionary of key inputs I could map each input directly to a
function. However there is no instance name I can call the function
on, as I create a thing, add a brain, and add handlers to the brain
like this:

player = Thing(26, 16, 'player')
player.brain = Brain()
player.brain.add_handlers(
                            commandHandler(player.brain, player),
                            keyboardHandler(player.brain),
                            fovHandler(player.brain))


So what I'm wondering is how to reference the instance in each brain's
list of handlers when I want to map something like a key input to a
command, or what a better way might be to structure the code.









More information about the Python-list mailing list