Since some discussions a while back with Social these features were mentioned.
Added these just the other day (since rc3), so youll need a recent build to test.
Both functions match existing dictionary methods.
These 2 functions work for GameObjects and GameLists
ob = sce.objects.get(“OBfoo”) # OBfoo or None
ob = sce.objects.get(“OBfoo”, ob_default) # OBfoo or ob_default
if sce.objects.has_key(“OBfoo”): print “blah”
This also works just like hasattr for gameObjects
if hasattr(ob, “prop”): print ‘hello world’
-> if ob.has_key(“prop”): print ‘hello world’
prop = ob.get(“someProp”, 0) # always return the prop, 0 if it doesnt exist.
— Will mention in release logs but thought its worth posting here too.
either your description is wrong or there is a bug!
if ob.has_key(“prop”): print ‘hello world’ doesn’t work for me. I have to write
if ob.has_key(prop): print ‘hello world’
and the warning is misleading
and using hasattr gives a warning about val = ob.prop -> val = ob[“prop”], from your point of view it might be correct (it has to do with props…) but to me hasttr doesn’t look like val = ob.prop. When it says where it is it doesn’t use the word line, it just gives a number. A bit inconsistent from the other warnings.
For the first one you’ll have to decide if it is a bug or not, the second one will be filed as a bug soon if there is no reply.
ob.has_key(“prop”) vs ob.has_key(prop) - simply depends on your script, if prop is defined as a variable or if you have a property called prop.
hasattr is checking for attributes, since using a property as an attribute is being deprecated use of hasattr() should be removed. The misleading error is unfortunate but since python gets the attribute internally there is no way around this. - if you call a python function that gets the attribute it would cause the error too though give a more useful line number.
There is a bug in gameObject.get(“prop”) - which would not convert a float into a python float - for 2.49, was fixed a day or 2 after released As were the attribute warnings.
I could add in the warning that hasattr() should be relplaced by .has_key() for some of these confusing cases.
Didn’t know about has_key being deprecated, bummer. though for our purpose has_key() is faster and more convenient for us.
The problem with making gameObject act like a sequence is that its not.
for prop in GameOb:… # can work
if prop in GameOb: … # too
if len(GameOb): # works but gives BIG problems…
if ob: # now checks len() and will fail on objects with no props.
So basically if you really want all the sequence operations on properties we need gameOb.properties and THAT can be used as a sequence, but not GameObject.
“prop” in gameOb will be a bit faster then gameOb.has_key(“prop”) since it doesnt have to allocate a python function and free it each time, though this is probably very minor compared to the speedup from hasattr to has_key().