On May 5, 7:55 pm, Dave Angel <da...@ieee.org> wrote: > George Oliver wrote: > > On May 5, 11:59 am, Dave Angel <da...@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. > > >> > > >>player.brain.add_handlers( > >> commandHandler(player.brain, player), > >> keyboardHandler(player.brain), > >> fovHandler(player.brain)) > > You're executing commandHandler, and passing its return value into the > add_handlers() method. So commandHandler is a function, not a method, > and what is its relationship to anything that actually processes commands?
It seems to be a factory function. > Sorry, your pseudo-code is so far from real code that I can't figure out > what you're doing. So I guess I can't be any help till something else > turns up to make it clearer. Maybe it's just me. I think it's you--and probably a lot of other people who haven't ever written games. No offense. As someone who's written games before I will tell you that George's pseudo-code is not far from real code. His overall approach is fairly typical of how games handle input, although there are some problems in the details. Carl Banks -- http://mail.python.org/mailman/listinfo/python-list