Here's my basic code:
import bpy
import bge
from bge import logic
cont = bge.logic.getCurrentController()
scene = bge.logic.getCurrentScene()
scenes = logic.getSceneList()
hud = [scene for scene in scenes if scene.name == "hud"][0]
obj1 = scene.objects["obj1"]
obj2 = hud.objects["obj2"]
I get an error when I define obj2
:
However, there is no error when I define obj1
.
I've found some more confusing problems related to this, and some of them could help solve this question. For example, if I define obj1
in a different way (bpy.data.objects["obj1"]
), I will get an error when calling a property (obj1["prop"]
).
Why can't I define an object from another scene when the scene is defined and the same method of defining is used as the current scene (that works)? And what is a solution?
Thank you.
EDIT: Ive used an answer for my code, and it raised another issue. Here are the details:
Code:
import bge
from bge import logic
for scene in logic.getSceneList():
if scene.name == "main":
main = scene
if scene.name == "hud":
hud = scene
player = main.objects["player"]
hud_obj = hud.objects["hud_obj"]
I have no idea why, but this is not working. It works if I remove the defining of the main scene and objects from the main scene. In other words, when I use more than one scene, this does not work. Why is this happening? How can I call objects from other scenes when I'm defining more than one scene? Why does this work when I call objects from main but not from hud?
NOTE: I printed all the active scenes, and both scenes were printed. If I remove either part of the code (definition of main/definition of hud) it will work, but it won't with both. Also, for other reasons, I am not using scene = logic.getCurrentScene. Even if that works, the problem will come back when I use 3 scenes. The console is telling me (with the code above) that "hud" is not defined. Furthermore, if I define the objects under the definition of the scene (e.g. main = scene), it will work. However, this raises a problem: I cannot call the object afterwards. In other words, it seems as the bge is forgetting the hud after I've defined it explicitly.
Picture:
I have a scene named "main" and "hud"