[Author Prev][Author Next][Thread Prev][Thread Next][Author Index][Thread Index]

[pygame] Ridiculously simple way to seperate game-object code



http://people.ucsc.edu/~jhofmann/programmables.py

More "inspired by Megazeux" code.

I could have used this as far back as last year, yet I
never realized I could do it this way until trying it
out tonight. Neither have I seen it recommended as a
solution anywhere.

What this little piece of code does, is set up an
object with a method to execute Python code through a
list of strings. It limits itself only to 30 lines at
a time, so that no one object can dominate the process
at any time. When it reaches the end of the program,
it does nothing. The test code shows how you can set
up a simple loop by moving the line counter backwards.

It could be made far more sophisticated and
user-friendly, but it's a great proof-of-concept hack.

Advantages:
-Game objects are less strongly tied to your own code;
they can do normal behaviors in the method code and
then in addition execute other stuff.
-Cycle limitations make per-frame control easy by
adding functions that immediately kill the program:
i.e. something like walk("west") will move the object
and then set the remaining cycles to 0. So a program
that has an object walk west five times will do it
over five frames and you don't have to track any
additional variables. Or if you want to kill the
program for multiple frames(maybe you have some kind
of AI that only needs to work occasionally), it can be
done with a small mod to the program execution code.
-Because the program data has no compilation stage,
you can set up a self-hosting game IDE, where you edit
object code within your level editor program and never
have to restart.
-It's one of the better-performing ways of adding a
"scripting language" to a Python game since it's
running in Python...and in the same instance your game
runs in.

Disadvantages:
-Behavior is different from normal Python code because
of the way exec works; If-blocks and the like have to
be joined in this ugly way:
'if True: print "first line" ; print "second line"'
-If your game is meant to be modded, people can use
this functionality maliciously. It's a wide-open
security loophole and it would be very
hard(impossible?) to filter out all "bad" lines:
'while True: pass' = dead game.
Or worse, 'import os; os.removedirs("a/b/c")' is a
start towards killing your hard drive if the user
doesn't have good permissions in place.

However, your game would probably have to be very
popular before anyone bothered....after all, users
download and run random code all the time - it could
all be harmful, but most isn't :)

Exec can also be used with whole files or code
objects, but that loses the granularity of this
approach.

This post took a lot longer to write than the code
did. :/ Hopefully someone finds it useful though. I
needed to write it to think through all the
implications of this system, myself.

__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com