Accessing Properties through Python

Hey all, I’m trying to figure this out. I have one script that handles several objects, but needs to have different outcomes depending on which object it is attached to. It checks the object’s game property to differentiate between objects. Would something like this be correct? Is this how you access the game property of an object?

if obj["BlueKart"]:    #if object has "BlueKart" property
    #do something that applies to blue kart

elif obj["GreenKart"]:    #if object has "GreenKart" property
    #do something that applies to green kart

I’m using this for the AI karts in my racing game. Instead of having a separate vehicle wrapper script for each kart, I want to have one script which changes the vehicle wrapper settings depending on the kart it’s attached to.

no, that just checks if the boolean value of the property is True.
If it doesn’t exist, it throws an error.
You should remember that object properties are stored on a dictionary
So, you can use ‘in’

if ‘BlueKart’ in obj:

elif ‘GreenKart’ in obj:

however, you could add a property: Kart, and set the value to BlueKart etc

Hmm, neither method seems to be working right, though it may be due to other complications. With the second method you suggested, does the value of the property have be in quotes? And the property must be a string property, right?

 if kart["KartColor"] = "Blue":
    #do whatever

You can have a look at the visual vehicle wrapper.

to access properties:

value = gameObject["propertyName"] 

throws an KeyError if the property does not exist.
You can as agoose’s suggest check for existence first:

if "propertyName" in gameObject:
  value = gameObject["propertyName"] 

or you catch the Error:

  value = gameObject["propertyName"] 
except KeyError:
  #property not present

KX_GameObject already provides methods that allow an “error-free” access:

value = gameObject("propertyName", defaultValue)

If the property does not exist it returns the default value. If no default value is given it returns None.

  • It does not add a property! -> you need to explicit create the property by assigning a value.
  • a property might already contain the default value -> you can’t tell if the property exist or not
  • default values are created even if not needed -> do not use complex or large default values e.g. dictionaries or lists.

yes , ensure which all car have the property “KartColor” and a “string” as value


and check the consolle (I know the setup is much complex!)

anyway , if you use this method ,(if kart[“kartColor”]==“Blue”) this allow to use different script I think

in this case can be much better ,add different properties to the car instead
then using this property in the script


gas = own[“gas”]

(where gas is a property which you have add to the obj)
this way the script is the same for all car…

firstly read the console - it is your friend. Secondly, use double equals for comparators.

ops , yes double == to comparison ,single = to assignement

 if kart["KartColor"] == "Blue" :
    speed = 55