How to add object from another scene and use and object in bge?

Hello friends,
If I will explain what I want to do;
When I right click on the cube, the camera changes and “HUD” is added to the main scene. It is enough to right click anywhere on the screen to change the camera again. But instead of this, I want to change the camera by pressing the “exit” button that I transferred from the “HUD” scene.
For this;
I want to add the “exit” object in the HUD scene to the “Scene” scene.
However, after I added the “HUD” scene with “bge.logic.addScene”, when I do “getSceneList”, the HUD scene not added, and when I print it on screen, it returns “None”.
Is there a way to do that?
In summary:
I want to use “added object from another scene” for the camera to change for the second time, but I cannot. Blender does not allow the object to be added. I get the warning “There is no object on the list”.
Thank you from now.

Note: The sample blender file is available below. Maybe it will be clearer.

Attachments

add_object_another_scene.blend (494 KB)

You can communicate between scenes with messages.

When exit button is clicked, send some message to the Cube.

Then when the Cube receives that message, he does whatever needs be done. Good luck.

You sure you’re referencing the right scene with addObject? Try printing on console the list of objects that the scene contains. print(scene.objects) if I recall.

last i checked, this cant happen.

try: scene.addObject(hud.objectsInactive[“name”], owner, 0)

i recently found text doesnt show when a script called from main scene tells the hud scene to addObject on the hud scene.

best to have the hud script run independent on the hud scene, while receiving messages through global/module level variables.

Wild guess: The scene is “not added” because addScene doesn’t work immediately. If you add the scene and then try to interact with it right away bge says there’s no such thing as the scene you’re looking for. A common approach: sort thru the list, check that the scene exists, and if not, add it. So:

for scene in bge.logic.getSceneList():
            if scene.name == 'HUDSceneName':
                #doStuff
            else:
                #addScene

It does feel kind of dull having to use a loop here though, perhaps someone else has a better idea. But this works just fine.

Thank you very much, this never came to mind, I will definitely try.

Thank you very much, and if I find a better solution, I will definitely communicate with you.

I think the best is to send a message, thank you very much for your help.

i store scenes in module variables.

global SC_HUD
if SC_HUD != None:

I do too…my player creates lists of these things when he initiates…so I am not looping through a lot of useless stuff. It is faster especially if you find yourself doing a lot of loops…which you should try not to do…but it happens.

Do you have an example file that you can share with me about this?

Mouse button Sensor AND Mouse over sensor will help you on detecting the moue click.

You can’t dynamically add objects to scenes (unless you use LibLoad). But you can create copies of inactive objects. Inactive objects are in an hidden layer (Reason: when you delete the last active object you would not be able to create a copy anymore).

So place your “exit” object in an hidden layer and you can add copies of that to the same scene (no you can’t add objects from another scene)

As stated above, the scene will be loaded after the current frame (before the next frame).