What the get() function does?

Hi, there is a get() function for KX_GameObject. In the docs: https://www.blender.org/api/blender_python_api_2_75_3/bge.types.KX_GameObject.html

Here I copy the doc of this function:

get(key, default=None)
Return the value matching key, or the default value if its not found. :return: The key value or a default.

So my question is what are that keys? Where can I find them? I think they can replace lots of functions like getActionFrame(), isPlayingAction(), getVelocity(), and all the others get functions.

Are you trying to know whether a certain Key is active? If so you can just do,

wKeyactive = bge.logic.KX_INPUT_ACTIVE == keyboard.events[bge.events.WKEY]

   if wKeyactive:
      (What ever you want here)

The keys mean the object’s properties, those ones that you get by using object[“someProperty”]. Example, you have an object that DOESN’T have a property named = somenthing.

owner= controller.owner
property = owner.get("somenthing", None)
print (property)

Then, It will print a None value (second argument of the “get” function), because the “somenthing” property was not found, otherwise it will return the value of “somenthing”.

as far as i know it just gets a property from an object. if the property doesn’t exist it will use a supplied default(will return ‘None’ if no supplied default)



obj['prop'] = 0.2 # can set this in GUI also

property_value = obj.get('prop', 1.2)


this code would get the value of ‘prop’ and place it in property_value. if ‘prop’ didn’t exist, it would give property_value the value of 1.2. i’m not sure what else you can do with this function.

All game objects have a dictionary attached to them. This dictionary holds all the game properties you assign to the object using the Logic Editor UI in addition to any thing else you might add to it in Python scripts.

Dictionaries are Python objects used to store data and provide fast access to it. An entry in a dictionary is called an item and each item consists of a key-value pair. To put an value in a dictionary, you have to give it a key that you’ll use for retrieving the value from the dictionary later. The key can be any Python object that is hashable (don’t worry if you don’t know what that means). In the case of game properties, the key is the property name string and the value is whatever value you assigned.

Most of the time people access dictionary values using brackets. However, dictionaries also have a get() method that essentially does the same thing but does not raise an error if the key you provide doesn’t exists and allows you to specify a default return value. Here’s an example:

import bge
cont = bge.logic.getCurrentController()
obj = cont.owner

# lets get the property called 'myProp' from the object
# using brackets
propValue = obj['myProp']

# using the get method
propValue = obj.get('myProp')

# if you try to use a key that does not exist
fakeProp = obj['keyThatDoesNotExist']
# this will raise a KeyError and will cause your script to fail if not handled properly

# or you can handle non-existing keys with get()
fakeProp = obj.get('keyThatDoesNotExist', 0)
# in this case, fakeProp = 0 since the given key does not exist and I specified 0 as the default value

Note that all of this has nothing to do with other “get” methods like getActionFrame(). Those do exactly what they say in the docs.

Oh thanks! I was confused, now I understand :stuck_out_tongue:

Thanks! I read it and it help me