Add a object that is in the main scene in a overlay scene?

I make this blend for show the problem:
objectFromAnotherScene.blend (102 KB)

An empty run this script in the overlay scene “overLayScene”:


import bge
overLayScene = bge.logic.getCurrentScene()
mainScene = [scene for scene in bge.logic.getSceneList() if scene.name == "mainScene"][0]

objectFromAnotherScene = mainScene.objectsInactive["objectFromAnotherScene"]
overLayScene.addObject(objectFromAnotherScene, "spawn")

The purpose is spawn the object “objectFromAnotherScene”(it is in the main scene) in the overlay scene “overLayScene”, it use the object “spawn”(it is in the overlay scene) for spawning.

At the start an empty add the “overLayScene”.

The problen is the console returns the error “object must be in an inactive layer”, but the object “objectFromAnotherScene” is already in an inactive layer.

Trying to make it in the “mainScene” with another script produce the same error.

What i am trying to make is spawn an object (this object is loaded using LibLoad, so it appears in the main scene, i guess it happen because the script that do the dynamic loading is in the main scene) in a overlay scene when the player pickup an item.

It is really important for my fix the problem! it is stopping me, Thanks and I hope someone can help me.

Even when I try to spawn the object executing the script in the main scene the errors still appears:

import bge
overLayScene = [scene for scene in bge.logic.getSceneList() if scene.name == "overLayScene"][0]

spawn = overLayScene.objects["spawn"]
overLayScene.addObject("object", spawn)

Blend with the last script: objectFromAnotherScene.blend (103 KB)

although you can spawn an object in scene 1 from code in scene 2 like youve done. If the object resides in scene 2 on a hidden layer, you cant load it into scene one unless it come from an external source (the code is looking in hidden layers on scene 1 for the object). IOW the object cant exist in scene 2 if your trying to load it into scene 1, You can call it, but it would still have to exist in a hidden layer on scene 1. you cant just move objects accross scenes unless your leading them externally. FME

From what I can understand your exectuting your script in scene2(overlay)

objectFromAnotherScene = mainScene.objectsInactive[“objectFromAnotherScene”]

its going to be looking for an object in a hidden layer on scene 1(mainscene), not scene 2, and it looks like the object you are trying to add is in a back layer of scene 2(IOW the object cant exist in a layer on scene 2…if youf saying its an inactive object on scene 1, it cant be an onject on layer 2 in scene2, and be loaded into scene 2). It cant load a hidden object that is on layer 2 in scene 2, into scene 1, using the method your trying. You CAN load external objects into multiple scenes, and they can all co-exist at once happily, but they must be external. Libloaded or whatever. Your using objectsInactive correctly it looks like, just trying to load Sword in layer 2 on scene2, into scene 1. And it cant work that way. Basically it seems like your trying to use one object across scenes, and you cant do it that way. If they are libloaded externally, then libload it again as you need it. I cant help with libload stuff, as Im not that good yet, Hope that helps.

Thanks, now i understand, so i can’t add a object that resides in another scene(only call KX_GameObject), unless it is libloaded(or if it comes from an external source), right? The problem is even if the object is LibLoaded(the file that is LibLoaded is already set to an inactive layer), the problem error still appears.

When I use LibLoad the objects only appears in the main scene, how I know it? i check the objectsInactive, and the LibLoaded objects are inside main scene, so it is the same problem, Maybe the LibLoad load the objects only in the scene that execute the script, I did not check, but if it is true, execute the LibLoad in the over lay scene will no help me, because there is objects in the file that I will add also in the main scene.

Although, i’m thinking in use an simple object that resides in the overlay scene, spawn it(in the same scene), and replace the mesh with the mesh of the object that resides in scene 1(that I was trying to add), and copy his transforms, but I still want to know how make what i was trying

Since there seems to be some confusion any object belongs to only one scene. You add them to one scene only via scene.addObject().

There are active and inactive layers which are read when you start the game. For any scene, any object in any active layer will be in scene.objects and any object in inactive layers will be in scene.objectsInactive.

It doesn’t matter which scene you start the script from. The only thing that matters is how you get the reference to the scene that you use with scene.addObject. If you use bge.logic.getCurrentScene you get the scene the script is called from. You could store your scenes somewhere like bge.logic.inventoryScene and bge.logic.gameScene where they are easy to access.

When you link / libload the layer state is read from the original file. So if you have layer 1 active when you start the game and it reads external blend file where object is on layer 1 (even if it’s not active in that .blend currently) it will give error when adding objects. If you have linked object in an inactive layer the original layer in the source .blend still counts when using addObject().

There are active and inactive layers which are read when you start the game. For any scene, any object in any active layer will be in scene.objects and any object in inactive layers will be in scene.objectsInactive.

I know that!

It doesn’t matter which scene you start the script from. The only thing that matters is how you get the reference to the scene that you use with scene.addObject. If you use bge.logic.getCurrentScene you get the scene the script is called from. You could store your scenes somewhere like bge.logic.inventoryScene and bge.logic.gameScene where they are easy to access.

In the script I have acces to the another scene using the ge.logic.getSceneList().

When you link / libload the layer state is read from the original file. So if you have layer 1 active when you start the game and it reads external blend file where object is on layer 1 (even if it’s not active in that .blend currently) it will give error when adding objects. If you have linked object in an inactive layer the original layer in the source .blend still counts when using addObject().

But in another file(this file has nothing to do with it), it has objects that are in some layers(counting up and down: 1,2, 3, 4, 10, 19), and the game file has activated some layers(counting up and down: 1,2) that in the another file has objects in it, when the file is loaded by LibLoad, these objects in the loaded file appears as inactive objects, despite these are in the same layers as the game file.

I check the file that contain the object that i want to spawn in the overlay scene, there is no objects in the layers that are activated in the game file. So, I think maybe is not the problem.

Can someone make a example?

PD: I prefer spawn the object using a script in the game scene, and not in the overlay, like the post 2#.
PD2: …overlay aparently is only one word…:smiley:

In the script I have acces to the another scene using the ge.logic.getSceneList().

It doesn’t matter, you still can’t add the objects from there to another scene. The addObject only looks for an object in the same scene. I guess this also links to how BGE handles preparing renderer with light and mesh data.

Well, I’m going to just spawn an object that is in the overlay scene and replace his mesh…anyway Thank you! sometimes I struggle to understand some things …

I guess this also links to how BGE handles preparing renderer with light and mesh data.

Yes, you’re right, I can’t replace a mesh in the overlay scene with one that is in another scene. I don’t have idea of what I will make.

Dude. Just link the object from external file to both scenes.

Actually that’s what I most wanted to avoid, because the idea is can “create new content” easily, the concept that I use for items and trees(and there will be more things like craftings, maps) is basically read CFG files that have the info of every item, tree, etc, as the id, file path, etc, and load the file when it is needed, So, in this moment I have a script that load all the cfg files in some paths and store their info in the globalDict. The need of link every item, tree added to the game I do not like much.

The object that I was trying to add is the representation of the item in the inventory(it is no a plane, it is a mesh), so the only thing that I can make is make the inventory with just simple icons(plane with a texture) for represent the items OR link every object.

Well if you’re using libload then simply load into both scenes.

But either way it is a bit heavy for just inventory icons. They are lower detail and doesn’t make sense to have them hogging all the resources. You can’t make use of the dynamic loading when you need to keep all the meshes loaded for inventory access anyway.

I’d recommend having just placeholder blank/colored icons and later make a .blend containing lowpoly versions of all your icons. You can add something like “_inv” to the end of the object name to distinguish from the actual objects. It’s what I’m doing for my project.