I write some Python code, and include something like o.sample_property = 5, etc, and I get an error in the console stating that there is the AttributeError, as if the script misses all properties. This has stumped me, because o = c.owner gets the actual object the script is attached to and everything in it, and c = logic.getCurrentController() gets the controller the object has the script on. I am on Blender 2.79c, and this is a problem for me. I often tend to abbreviate things like for example, from bge import logic as g or l, etc. So, I am asking, have any of you ever run into this problem? How to fix this?? Script also misses sensor even when it is connected sometimes…
o[“sample_property”] = 5
It seams you are referring to pre-2.49 API. Before 2.49 properties were accessed via attribute. With 2.49 that changed to key access as edderkop shows in post#2.
Btw. inventing these short abbreviations is no good style. You make your code difficult to understand and maintain.
Thanks. I had already looked it up on the internet by going to this handy site: http://www.tutorialsforblender3d.com
Turns out, it goes like this:
import bge cont = bge.logic.getCurrentController() # get object that controller is attached to obj = cont.owner
property = “sample_property”
value = 0
what it does is try to grab the property if it does not exist or falls into an error it will grab the default value, so the value you are setting is the default value for when it can’t get that property.
to directly set a property:
obj['property'] = value
to use a property
some_variable = obj['property'] #or property_default_value = False # just the default value you want to use if it does not exist some_variable = obj.get(property, property_default_value)