Getting a property from another object in the scene

Hi I have a question. I know that I can access another object on the scene in python like this.

scene = logic.getCurrentScene()
object1 = scene.objects[“object1”]

Is there a way that i can get a property from object1 also and establish it as a variable?

Many thanks

You can get a property from object1 like this:


prop = object1["prop_name"]

If object1[“prop_name”] is a reference to a mutable python object, the newly created prop variable can be treated as a reference to the same exact thing (because that’s what it is). However, if object1[“prop_name”] is an immutable object (int, string, float, etc), then prop is simply a copy of the original, which means that the original won’t reflect the changes made to the newly created prop variable.

but can I change the property by going:

scene = logic.getCurrentScene()
object1 = scene.objects["object1"]
prop = object1["prop_name"]
 
if this happens
    prop = my_variable

and it change it on the other object

The prop variable is set to my_variable; object1[“prop_name”] remains as it was.

i see…thanks for the help

You can assign or retrieve properties of another object.
For example the following script is called from a python controller that belongs to a cube object and sets/retrieves the properties of a sphere.

<b># This script launched by a python controller of a Cube object</b>

from bge import logic
scene = logic.getCurrentScene()
sphere = scene.objects["Sphere"]
sphere["SphereProp1"] = 5.          # assign a value to SphereProp1
prop2 = sphere["SphereProp2"] # read SphereProp2