3
$\begingroup$

I have a game with multiple levels made up of separate scenes. I'd like to add (at runtime) objects in an "assets" scene containing some generic objects that will be used in each level.

After unsuccessfully trying bge.logic.addScene("assets") then scene.addObject("object_from_assets_scene"), I tried using a "loader" scene which would load the assets scene first (to allow for the async loading), then switch to the new scene (with scene.replace()).

Despite the scenes appearing in bge.logic.getSceneList(), none of the asset objects are in them and the main level objects aren't visible.

Is there any way to add an object from another scene?

$\endgroup$
1
$\begingroup$

You cannot add objects across scenes. Scenes are completely seperate. What you can do is you can use LibLoad to combine two blend files. So if you have your assets in one blend (assets.blend), you can use: bge.logic.LibLoad(path_to_blend, 'Scene') to merge the two scenes. If objects are on an inactive layer in assets.blend, then you can use addObject to add them when/where you want.

(note that the 'Scene' is to tell it to load whole scene, but does not tell it which. There is no way to tell LIbLoad which scene to load - it will do the one that the blend is saved with)

$\endgroup$
0
$\begingroup$

You can better import the same object in the scene(s) where you want to use them and just replace them into a layer you don't use in game. Like that you don't have to script anything and you can just use the edit object actuator.

$\endgroup$
  • $\begingroup$ Thanks for your answer :) The thing is I have quite a few scenes where these objects need to be used. If I copy them into every scene I'll have to re-copy them whenever I edit them later.. (I suppose I could link them.. but if possible I'd prefer to only store objects in a "level" scene if they are unique to that "level") $\endgroup$ – gandalf3 Dec 24 '15 at 2:24
0
$\begingroup$

would it be a possibility to link the object datablock to the active scene?

bpy.context.scene.objects.link(obj) Link the object to the current scene and make it active. Optionally, we can make the newly created object active or selected. This code is the same for all kinds of objects.

from: https://wiki.blender.org/index.php/Dev:Py/Scripts/Cookbook/Code_snippets/Three_ways_to_create_objects

i use this method to create my objects and link them to the active scene.

$\endgroup$
  • $\begingroup$ This question is about adding gameobjects from within the game engine. bpy is not available inside the game engine. $\endgroup$ – gandalf3 Mar 23 '16 at 19:01

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.