Question about using python as a scripting language

H

heavydada

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

Steve Lianoglou

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
 
T

Terry Reedy

heavydada said:
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
 
J

Jordan Greenberg

Terry said:
"heavydada" <[email protected]> wrote in message

Suppose you have a file actions.py with some action functions:
def hop(self): ...
def skip(self): ...
def jump(self)
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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top