Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Python (http://www.velocityreviews.com/forums/f43-python.html)
-   -   Question about using python as a scripting language (http://www.velocityreviews.com/forums/t362712-question-about-using-python-as-a-scripting-language.html)

heavydada 08-07-2006 12:28 AM

Question about using python as a scripting language
 
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.


Steve Lianoglou 08-07-2006 02:44 AM

Re: Question about using python as a scripting language
 
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


Terry Reedy 08-07-2006 03:07 AM

Re: Question about using python as a scripting language
 

"heavydada" <jlara_garduno@hotmail.com> wrote in message
news:1154910518.506581.26470@b28g2000cwb.googlegro ups.com...
> 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




Jordan Greenberg 08-07-2006 08:46 AM

Re: Question about using python as a scripting language
 
Terry Reedy wrote:
> "heavydada" <jlara_garduno@hotmail.com> 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



All times are GMT. The time now is 03:42 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.