Question about using python as a scripting language

Discussion in 'Python' started by heavydada, Aug 7, 2006.

  1. heavydada

    heavydada Guest

    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.
     
    heavydada, Aug 7, 2006
    #1
    1. Advertising

  2. Hi,

    > I was wondering how I can read
    > commands from the XML file and then execute them in the game.

    ...
    > 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.


    One thing you could do is use the eval or compile methods. These
    functions let you run arbitray code passed into them as a string.

    So, for instance, you can write:
    my_list = eval('[1,2,3,4]')

    and my_list will then be assigned the list [1,2,3,4], moreover:

    eval("my_list.%s" % "reverse()")

    or ... even further .. :)

    command = "reverse"
    eval("my_list.%s()" % "reverse")

    will reverse my_list

    Is that something like what you're looking for?

    -steve
     
    Steve Lianoglou, Aug 7, 2006
    #2
    1. Advertising

  3. heavydada

    Terry Reedy Guest

    "heavydada" <> wrote in message
    news:...
    > 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
     
    Terry Reedy, Aug 7, 2006
    #3
  4. Terry Reedy wrote:
    > "heavydada" <> wrote in message

    <snip>
    >> 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)

    <more snipping>
    > Terry Jan Reedy


    Another convenient way if, for some reason, you're not creating objects
    for your creatures would be using a dictionary to look up functions, like:

    def hop(x):
    return x+1
    def skip(x):
    return x+2
    def jump(x):
    return x+3

    actionlookup={"hop": hop, "skip": skip, "jump": jump}
    action=actionlookup["hop"]
    action() #this will call hop()

    Please note that I'm only including this for completeness, for any
    larger project (or medium sized, or anything more then a few lines,
    really) this gets really unwieldy really quickly (imagine if you had
    thousands of functions! Madness!) Terry's suggestion is a much better
    solution then this. If this looks easier, consider changing the rest of
    your program before kludging something hideous like this together.

    Good luck,
    -Jordan Greenberg

    --
    Posted via a free Usenet account from http://www.teranews.com
     
    Jordan Greenberg, Aug 7, 2006
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. popov
    Replies:
    3
    Views:
    532
    John McLaughlin
    Jan 23, 2004
  2. Ron Stephens
    Replies:
    23
    Views:
    2,855
    Ron Stephens
    Apr 12, 2004
  3. DaveInSidney
    Replies:
    0
    Views:
    421
    DaveInSidney
    May 9, 2005
  4. Delaney, Timothy (Tim)

    RE: Question about using python as a scripting language

    Delaney, Timothy (Tim), Aug 7, 2006, in forum: Python
    Replies:
    7
    Views:
    347
    Wildemar Wildenburger
    Aug 10, 2006
  5. Chris Lambacher
    Replies:
    2
    Views:
    320
    Simon Forman
    Aug 9, 2006
Loading...

Share This Page