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

Re: [pygame] Scripting language



On 12/18/06, Brandon N <kordova@xxxxxxxxx> wrote:
   Anyway, I am using Python as the development language for both the
client and server in a project I am working on. Now, I want to be
able to expose certain functionality for scripting other than the
internal development kind I have been doing with Python. That is,
users can write scripts that affect the behavior of their avatars in
the world.

Hmmm... if you want to have users able to write these scripts and you
want to have what they are capable of to be "sandboxed", I'd recommend
a system where scripting is done by creating instances of classes that
represent logic and actions in the scripting system, and then making
an editor that allows you to build trees of these objects by picking
which object you want connected to another (using a drop down) and
then you edit their properties to change what the actions & logic do.
So like, when your game objects execute script (on events or whatever)
that would look something like this:
  self.script.execute(self, context)
and maybe the userused the editor to make the script property point to
an instance of the IfThenScript class instance, which you wrote to
have an execute method something like this:
  def execute(self, owner, context):
      if self.condition.evaluate(owner, content):
         self.then_clause.execute(owner, context)
      else:
         self.else_clause.execute(owner, context)
and the IfThenScript instance's condition and then_clause and
else_clause would all be instances of some other scripting class types
you wrote that the user hooked up using the editor

I like it because then there is no parsing or syntax issues to deal
with (i.e it's hard for the user to make something invalid vs. them
having to type out code), and it's very easy for users to learn what
they can do because they have to pick which class they can use in what
situation from a list. Finally, it's easier to show and manipulate
visually if you do your editor well.

Python is good for this too because of all it's reflection
capabilities (you can enumerate derived classes, get class types,
enumerate properties, etc.). In addition python already has
serialization (so you can pretty much just pickle and unpickle your
trees of scripting objects, although you may want to do some extra
work there for the sake of security)

The biggest cost there would be writing an editor for users to build
and modify your scripting objects.