How to access object attributes given a string

Santiago Romero sromero at gmail.com
Tue Feb 12 21:53:36 CET 2008


> > def ChangeAttribute( object, attribute, value ):
>    help(setattr)
>
> Help on built-in function setattr in module __builtin__:
>
> setattr(...) setattr(object, name, value)
>
> Set a named attribute on an object; setattr(x, 'y', v) is
> equivalent to`x.y = v''.

and

> Gary Herron write:
> You want getattr and setattr:

 Thanks a lot, that's what I was looking for.

> Dennis Kempin wrote:
> You are using a scripting language.. why not use python directly?

 Well, I have a .OBJ with something like:

# exec is the code executed when the player gets the object
ADDOBJECT:coin:x=10:y=10:screen=200:exec=\
   SETMAP(10,10,1);\
   SLEEP(1);\
   PLAYSOUND(get_coin);\
   IF FLAG(10);\
      SETLOOKING(left);\
      PLAYERSAY(blah);\
      SLEEP(2);\
   ELSE;\
      PLAYERSAY(blahblah);\
      SLEEP(3);\
   ENDIF;\

 That's a simple example of the language I've wrote for my game. Now
it's being "compiled" nicely and it works.

I just want to execute an small set of commands (PLAYSOUND, PLAYMUSIC,
WALKTO, PLAYERSAY, SLEEP and a couple more) ... do you think I can
write python code inside my object.exec (list attribute) loaded from a
file?

 Currently I'm compiling the above in a ( scope_level, function,
parameters ) list:

object.exec = [
[ 0, "SETMAP", [10, 10, 1] ],
[ 0, "SLEEP", [1] ],
[ 0, "PLAYSOUND", ["get_coin"] ],
[ 0, "IF FLAG", [10] ],
[ 1, "SETLOOKING", ["left"] ],
(...) ]

 I make some checks (as many resulting opcodes as input commands,
check of IF/ELIF/ELSE/ENDIF statements to see if all are correct,
right parameter types (currently only integer or strings), right
parameter number, and so on.

 Do you mean that I can replace my "language" by just python (defining
SETMAP, SLEEP and all those functions somewhere in my main source
code).




More information about the Python-list mailing list