controlling object from other scene

Hi,
I am new in python game engine scripting. I have a game with two scenes.
scene 1 and scene 2.
I want to use an object from scene 2 as actuator ( to move) an object from scene 1.
But i dont know how.

Well, I’m not too sure why you would need to but if you want to get scene 2 from scene 1 in python just use:

scenes = bge.logic.getSceneList()

This will return a list of your scenes, so you can get scene 2 by using scene_2 = scenes[1] or whatever the index of your scene 2 is. Then to get an object from scene_2 just use scene_2_obj = scene_2.objects and that will return a list of objects in scene 2.

So now you have your scene 2 object and your scene 1 object in the same script (assuming the owner of the script is your scene 1 object) you can now use each others sensors and actuators. However you will need to have scene 2 added as an overlay scene as far as I know. If its not overlayed it sits idle and nothing can happen in it (someone confirm?).

So that part of your script should look like this:

scenes = bge.logic.getSceneList()
scene2 = scenes[1]
scene2_obj  = scene2.objects

object_in_scene2 = scene2_obj['object_in_scene2']

EDIT:

So as far as moving scene1 object with scene2 object I’m not sure what you mean? But with this method you can do a whole bunch of whatever you want because you access to both objects in the same script. For example: obj2.position = obj1.position and/or orientation, scale, linearVelocity etc. Also the ability to access each others sensors and actuators.

I wrote a method to get an object from a given scene a while ago, feel free to use it:


# Get a given object from a given scene
def getFromScene(objectName, sceneName):
        for scene in logic.getSceneList():
                if scene.name == sceneName:
                        return scene.objects[objectName]

Once you have the object you can move it in whatever way you want.
For example:


position = [1.0, 1.3, 2.8]
obj = getFromScene('foo_object', 'bar_scene')
obj.worldPosition = position

Keep in mind that it is somewhat unintuitive to move an object from another scene, and it relies on both scenes being active, etc.
You may well have a good reason to do it, just think about it.

Thank all of you very much. It worked

Thanks to all of you it worked. :yes:

And i also want to know if an object (cube2) has a 2 sensors (for example mouse over and left click) and cube2 is in scene2. And in scene1 there is an object called cube1 with an actuator (for example motion). scene2 is an overlay scene with scene1.
Can i let cube1 move if i mouse over and left click on cube2

Yes. You just need to have both objects available in the same script like one of the methods above then:

cube2_m_over = cube2.sensors['Mouse_over']
cube2_m_click = cube2.sensors['Left_mouse']
cube1_move = cube1.actuators['Movement']


if cube2_m_over.positive and cube2_m_click.positive:
    cont.activate(cube1_move)

Thanks that was very helpful