Accessing Properties of Other Objects

Hey everyone,

Another question that I believe should pose a quick and easy answer (I hope!) In the game engine and using python, is there a way to access the game properties of another object? For example, if I have an object, Player, with a child Camera, and each has a game property (we’ll call them playProp and camProp.) From a script attached to the Camera controller, can I check and/or modify the values of playProp?

I just found the answer, almost immediately after posting this. For reference, I’ll post the solution below in case anyone else ever asks this question…

This can be done by getting the current scene, and getting the objects in that scene. From there, you can specifically get your object by name, and get the property from it like you would any other object.

from bge import logic

scene = logic.getCurrentScene()
objects = scene.objects
player = objects["Player"]
print(str(player['playProp']))

import bge
scene = bge.logic.getCurrentScene()
#get the object
obj = scene.objects["Object name"]

#Edit a property
if a.positive:
    obj["Property"] = "Value"

Thanks for the more elegant solution!

And they are related by parenting you can get the variable through a own.parent or own.children


#if you want a property from a parent
def get_parent_prop():
    par = own.parent
    par["property"] = "value"

#if you want a property from a child
#Note: for this to work no other child may have the same property
def get_child_prop():
    for i in own.children:
        if "property" in i:
            child = i
            child["property"] = "value"