7
$\begingroup$

How would I set a property value with python in the Blender game engine?

$\endgroup$

4 Answers 4

8
$\begingroup$

Here is one way of setting the property value of a game engine object (which has an int property by the name 'upkeycount')

I have setup the following logic on a cube object

Sensor:Keyboard('UPARRAOWKEY') <--> Controller:Python(increment_up_count.py) <--> Actuator('upkeycount')

and following is the code for increment increment_up_count.py

import bge

cont = bge.logic.getCurrentController()

sensor = cont.sensors['UPARROWKEY']

if sensor.getKeyStatus(bge.events.UPARROWKEY) == bge.logic.KX_INPUT_JUST_ACTIVATED:
    owner = cont.owner
    upkeycountAct = cont.actuators['upkeycount']
    value = owner.get(upkeycountAct.propName) + 1
    upkeycountAct.value = str(value)
    cont.activate(upkeycountAct)

Few variations of the above code can be found at https://gist.github.com/satishgoda/5682945

$\endgroup$
6
$\begingroup$

There is also the Python API for setting properties directly. Here's the example satishgoda used but without using a keyboard sensor, or a property actuator:

from bge import logic, events

def set_property(cont):
    own = cont.owner

    key_status = logic.keyboard.events[events.UPKEY] == logic.KX_INPUT_JUST_ACTIVATED

    if not key_status:
        return

    own['prop_name'] += 1

Replace own['prop_name'] += 1 with

own['prop_name'] = own.get("prop_name", 0) + 1

if it doesn't have the property by default, maintaining the same indentation as before.

Here is the latest Python API for Blender. It will point to the most up-to-date version.

$\endgroup$
0
$\begingroup$

The way to call or use property values in the Blender Game Engine is the same as in each of the other render engines. However, all of the properties have different names. For example, the rotation of an object is found by calling object.rotation_euler in Cycles and Blender-Internal, but object.worldOrientation in the Game Engine.

As far as I know, the 'hover over' trick to find property names unfortunately doesn't work with the Game Engine (it will give you the same values as Cycles/Blender-Internal). I haven't yet found a single resource that covers all of the Game Engine properties, but I imagine the Blender Wiki Documentation would be a good place to start.

$\endgroup$
0
$\begingroup$

The blender game engine's objects' properties are organized like a dictionary. So you can use the interface to set it, I am sure there is a function to do it similar to the __setattr__ built in and you can do this:

GameObject["newprop1"]=1
GameObject["newprop2"]="Hi there!"
GameObject["newprop3"]={"A":"B"}
GameObject["newprop4"]=[1,2,3,4,5]
GameObject["newprop5"]=True
GameObject["newprop6"]=3.14156

I think lists and dictionaries don't always behave as expected since properties aren't inteded to be used like this, updating and changing is difficult afaik but you can still use them otherwise and if you need to change them and run into difficulties you can always create temporary local copies, change those and then reassign the property with the new value.

The others should work fine though.

$\endgroup$

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .