How to access object attributes given a string
sromero at gmail.com
Tue Feb 12 21:53:36 CET 2008
> > def ChangeAttribute( object, attribute, value ):
> 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''.
> 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
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
Currently I'm compiling the above in a ( scope_level, function,
parameters ) list:
object.exec = [
[ 0, "SETMAP", [10, 10, 1] ],
[ 0, "SLEEP",  ],
[ 0, "PLAYSOUND", ["get_coin"] ],
[ 0, "IF FLAG",  ],
[ 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
More information about the Python-list