access properties with python

Hey, I need to know how to access the properties that I define(not things like position) in the BGE with python. Also, it would be nice to know how to access properties of other objects that aren’t the controller of the module.

Also, on a kind of unrelated topic, is there any way to change the velocity directly with python?

Thanks!

To access property of the object the python script is hooked to.

from bge import logic as gl
cont=gl.getCurrentController()
own=cont.owner
own["property1"]=some_value

To access property of the object “name_of_object” in the current scene.

from bge import logic as gl
obj1=gl.getCurentScene().objects["name_of_object"]
obj1["property1"]=some_value

:o

I tried using the first code to print a string property and this error came up in the console:

Python script error from controller “cont#CONTR#1”:
Traceback (most recent call last):
File “test”, line 1, in <module>

ImportError: No module named bge
Python script error from controller “cont#CONTR#1”:
Traceback (most recent call last):
File “test”, line 1, in <module>

ImportError: No module named bge

  1. Are you using Blender 2.5?
  2. Are you running the script in a controller (not with the ‘Run Script’ command)?

My OS is Ubuntu and I’m using the version that I got from Ubuntu software center. It appears to be around 2.49, but I’m not sure about that. I downloaded 2.57 for Linux, but I don’t know how I could run it from the terminal.

In both versions you can use

import GameLogic

with the above exampls replace the first line with


import GameLogic as gl

That worked! Thanks monster!

Now the get current scene isn’t working.:frowning:

what error do you get when gl.getCurrentScene() doesnt work.
also above obj1=gl.getCurrentScene add this code

print(gl.getCurrentScene().objects)

there is a spelling error in sagas code (post#2) it is supposed to be

getCurrentScene()

please check the API (2.49) and (2.5). The 2.49 is much better to read than the 2.5 Docs. Just make sure you do not use deprecated code (it is marked as deprecated).

Now it’s giving me this error:

Python script error from controller “cont#CONTR#1”:
Traceback (most recent call last):
File “test”, line 4, in <module>

KeyError: “CList[key]: ‘‘Cube’’ key not in list”

And here’s line 4:
obj1=gl.getCurrentScene().objects[“Cube”]

try this

obj1=gl.getCurrentScene().objects["OBCube"]

Internal objects names in 2.49 are preceeded by “OB”
In 2.5 they are not.

In general using object names in Python code makes the code very inflexible and error-prone. You might run into trouble if you have multiple objects withthe same name (via addObject or group inctantiaing). And you get a problem if the object is renamed and can’t be found.

It is fine at the beginning, but later you might want to go a step further:
Often you can get object references from other sources,

  • like sensors (hit object),
  • the controller owning object (obj.parent, obj.children).
  • You can even let objects register at a central location (e.g. GameLogic.globalDict[“myCube”] = own) after creation and extract it by another Python controller (e.g. cube = GameLogic.get(“myCube”)).

my experience was that i couldn’t (any more in 2.5x) assign dynamically new properties by: obj.new_prop = … or even obj[“new_prop”] = …
the way it worked for me is this one: obj.attrDict[“new_prop”] = … and once this property is set you can “ask” the property with either obj.attrDict[“new_prop”] or just obj[“new_prop”].