accessing objects in python

hi there,

i have a problem which i am sure some other people had before, but i did not find a solution by searching the forums.

how do i access other objects (and their sensors, properties, actuators…) from a PythonController? the getCurrentController().getOwner() allows only access to one object…

thanx in advance, corban

I’m very bad with Python but…no one else wants to answer!!!
So, try with the Copy Property Actuator
Or use a Ray or Collision sensor with a Python script with
getHitObject or getHitPosition or the like??!!

You can connect the Sensors & Actuators from Object2 directly to the Script of Object 1. If ou have the Owner of an Object, You have Access to his Propertys. (ObjectOwner.PropertyName = X)

Hope this helps :slight_smile:

You can also use a near sensor:

near.getHitObjectList() will return a list of all the objects that it can see.

or you can have all the objects you need to access attach themselves to GameLogic: GameLogic.obname = object
This must be done in a script the object runs. Then, on another object, you can call GameLogic.obname to get the object.

but is there no way to access from a PythonController all other GameObjects? in my game i need to send information from the player object to other distant objects (for example if the player did use a switch). at the moment, the only way i see to do this is to use ‘global’ variables (iE GameObject.switch1 = 1) and read this variables via python from the objects that should receive the information. but that means i need to spend resources, cause to read information from this variables i need to use an always actuator who ‘listens’ to the variables. to directly change the properties of a different object means i can use a property sensor and save resources. i think, if you want to save the state of a game you need to access also all GameObjects, did’nt you?

greetings, corban

message actuator can broadcast a property only when it changes (property changed sensor) to some or all other game objects with message sensors.

Good luck,

For more flexibility you can use a dictionary or a list on GameLogic, or use messages. Instead of GameLogic.switch = ob you can go GameLogic.objects[ob.getName()] = ob. I’ll try to explain a little better.

On each object:

c = GameLogic.getCurrentController()
ob = c.getObject()

if hasattr(GameLogic,"objects"):   #Check to see if GameLogic has the objects variable yet
   if GameLogic.objects.get(ob.getName()) == None:  #Check to see if we've put ourselves there yet
      GameLogic.objects[ob.getName()] = ob
else:  #Create the variable on GameLogic
   GameLogic.objects = {}

That code is very light, and will only really be doing anything the first two times it runs.

Now, if you want to change a prop on all objects:

for o in GameLogic.objects.values():  #Get a list of all objects
   o.prop1 = blah   #Set the property

This code is a little heavier, but you probably won’t be calling it every frame. You will want to set it up so you only call it when you want to save, or whatever you wanted to do. Even if you ARE calling it every frame, python is very fast, and it probably won’t slow you down enough to notice, unless you have a rediculous amount of object (which blender doesn’t handle well anyway).

To save the state of the game, you can do the same thing I showed you above (the second codeblock), and instead of setting the properties, you can read the objects position, orientation, and any properties you need, and save them.

thanx a lot.