Get Object's Properties


(Haunted-House) #1

Is there anyway you can automaticly get a list of all an objects properties with python?


(saluk) #2

Not that I know of:(


(Haunted-House) #3

Not that I know of:(

Did the programers think it wouldn’t be a useful ability?
Thats just sad… :frowning:


(saluk) #4

It probably just never came up. What you could do is something like have a string prop on an object that can tell a script what properties it has.

Say you have an enemy, and they have a health, a type, and an action props. Give them one more prop called “props” and make it a string. For it’s value, enter “[‘health’,‘type’,‘action’]”. Now, in any script that you want to see all the properties, you can use this code:


#The object is called ob
props = eval(ob.props)
for propname in props:
   prop = getattr(ob,propname)  #To see what the prop is
   setattr(ob,propname,value)  #If you want to change the prop to something

Make sense?


(xhyldazhk) #5

have you tried

props = dir(ob)

?


(gorgan_almi) #6

that would work but it return all the names of the functions like getMesh etc as well.


(saluk) #7

Oh, hahahaha, dir, of course. All you have to do is read through and find anything that’s not a known function. So on some empty somewhere in our file, give it no properties, and give it a python script:


c = GameLogic.getCurrentController()
ob = c.getOwner()

if hasattr(GameLogic,"defprops"):  #if gamelogic doesn't have this yet
   GameLogic.defprops = dir(ob)

Then, anywhere you want to get a list of an objects properties, you can go:


props = []
for prop in dir(ob):
   if prop not in GameLogic.defprops:
      props.append(prop)

Then props will contain all the properties of an object.

Call me stupid for forgetting dir:) (The answer to ALL python questions:) )


(Haunted-House) #8

dir(ob)

It’s not getting the objects properties, only the other stuff (getPosition, getOrientation ect.) :frowning:


(saluk) #9

You are correct. I thought I had tried this once before and it worked…

Oh well. I apologize for wasting anyones time because I didn’t test stuff before I post :-?


(Haunted-House) #10

How dare you waste my time like that, it’s time to pay you fool!! :x

 JUST JOKING!   :P 

It’s no problem saluk.
(I could have sworn I had found a way to do this before…oh well)