[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.python

Executing other python code

NIm

1/29/2008 12:18:00 AM

I'm working on a game, and I'd like players to be able to define thier
ships with scripts. Naturally, I don't want to give them the entire
program as thier romping ground. I would like to invoke a seperate
interpreter for these files, and give it a limited subset of the
functions in my game. What is the best way to achieve this effect?
6 Answers

Rolf van de Krol

1/29/2008 1:48:00 AM

0

AFAIK this can't be done with just python. You can use the C API of
Python to achieve this. I don't know the details of that, but I guess
you will need this (http://docs.python.org/ap...).

Rolf

Tim Rau wrote:
> I'm working on a game, and I'd like players to be able to define thier
> ships with scripts. Naturally, I don't want to give them the entire
> program as thier romping ground. I would like to invoke a seperate
> interpreter for these files, and give it a limited subset of the
> functions in my game. What is the best way to achieve this effect?
>

Diez B. Roggisch

1/29/2008 12:20:00 PM

0

Tim Rau wrote:

> I'm working on a game, and I'd like players to be able to define thier
> ships with scripts. Naturally, I don't want to give them the entire
> program as thier romping ground. I would like to invoke a seperate
> interpreter for these files, and give it a limited subset of the
> functions in my game. What is the best way to achieve this effect?

You might consider spawning a process and using Pyro to communicate. I've
done that before and it worked pretty well.

Diez

Arnaud Delobelle

1/29/2008 1:26:00 PM

0

On Jan 29, 12:18 am, Tim Rau <bladedpeng...@gmail.com> wrote:
> I'm working on a game, and I'd like players to be able to define thier
> ships with scripts. Naturally, I don't want to give them the entire
> program as thier romping ground. I would like to invoke a seperate
> interpreter for these files, and give it a limited subset of the
> functions in my game. What is the best way to achieve this effect?

One simple solution would be to forbid import statements in the
scripts, to import the scripts as modules and inject whatever
functions you want them to be able to use in the module's namespace.

So:

====== script.py =======

def play():
if enemy_is_near():
fire_gun()
else:
move_forward()

=======================

====== Game engine ====

scriptobjs = [enemy_is_near, fire_gun, move_forward, turn_left,
turn_right]

def getscript(scriptname):
script = __import__(scriptname)
for obj in scriptobjs:
setattr(script, obj.__name__, obj)
return script

def playscript(script):
script.play()

=======================

Something like this. Obviously this is over simplified!

--
Arnaud

Martin Skou

1/29/2008 2:22:00 PM

0

Over-simplified yes, but it will work!

Python is beautiful :-)

Stefan Behnel

1/29/2008 2:46:00 PM

0

Hi,

Tim Rau wrote:
> I'm working on a game, and I'd like players to be able to define thier
> ships with scripts. Naturally, I don't want to give them the entire
> program as thier romping ground. I would like to invoke a seperate
> interpreter for these files, and give it a limited subset of the
> functions in my game. What is the best way to achieve this effect?

Depends. If you are concerned about security, this will be hard to achieve in
Python. I imagine that this could be related to cheating prevention.

If you are more concerned about namespace polution and making only a well
defined API available to the scripts, go the module-import way and hand some
API object over, either through an initialisation function or by injecting it
into the module namespace, as Arnaud suggested.

Stefan

guyon.moree@gmail.com

1/31/2008 1:33:00 PM

0


> One simple solution would be to forbid import statements in the
> scripts, to import the scripts as modules and inject whatever
> functions you want them to be able to use in the module's namespace.


how do you 'forbid' imports?


Guyon
http:...