"heavydada" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > I'm writing a small game in python and I need to be able to run some > scripts inside the game. In the game I have these creatures each with > some attributes like name and weight and an action. Right now I'm > saving all this information in an XML file, which I parse whenever I > need it. I can handle the attributes like name and weight because these > are just values I can assign to a variable, but the action part is what > has me stumped. Each of the creatures has a different action() function > (as in, each does something different). I was wondering how I can read > commands from the XML file and then execute them in the game. I read a > document that talked about this, but it was written in Visual Basic and > used a method called callByName or something like that. It could call a > function simply by sending the name as a string parameter. I was > wondering if there was an equivalent in python. I just need some way of > being able to read from the file what function the program needs to > call next. Any help is appreciated.
Suppose you have a file actions.py with some action functions: def hop(self): ... def skip(self): ... def jump(self) And a creature data file with entries with a field such as actionname = 'skip' and a method of converting an entry for a creature into a creature class instance. Then the action method for the creature class could look something like import actions.py class creature(whatever): def action(self): return getattr(actions, self.actionname)(self) The is one way to 'call by name' in Python. Terry Jan Reedy -- http://mail.python.org/mailman/listinfo/python-list