1
$\begingroup$

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:

error

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:

pic2

I have a scene named "main" and "hud"

$\endgroup$
  • $\begingroup$ In your last edit you say you have a scene named "hud". Well, logic.getSceneList returns a list of scenes that are currently running. Python is telling you that the hud variable is not defined, wich means that it hasn't been created becouse no active scene with name "hud" existed at the time. Put a print(logic.getSceneList()) just before the loops and you should see that one scene is missing. And by the way, you should convert those two loops into one. $\endgroup$ – elmeunick Jan 5 '16 at 14:19
  • $\begingroup$ Oh! I'll make a new question on it. Thanks ;) $\endgroup$ – blackhole Jan 6 '16 at 0:43
1
+50
$\begingroup$

I'm not sure exactly why your code is not working. Maybe you have incorrectly referenced your HUD scene? I'll give you an example of what has worked for me in the past, and it shouldn't give you any trouble.

Perhaps you should try it like this.


from bge import logic

### Find the main scene
hud = None
for scene in logic.getSceneList():
    if scene.name == "hud":
        hud = scene
        break

cont = logic.getCurrentController()
own = cont.owner
scene = logic.getCurrentScene()

obj1 = scene.objects["obj1"]
obj2 = hud.objects["obj2"]
$\endgroup$
  • $\begingroup$ I've edited the question. I found a problem when using your solution. Thanks though. $\endgroup$ – blackhole Jan 5 '16 at 5:03
2
$\begingroup$

Do not use bpy in BGE, it uses blender data that is not accesible while running the game.

But that's not the problem, that's just to avoid future problems. It's not possible to know exactly whats happening without the blend file but you can check the following list:

  • An object with name "obj2" exists in a scene with the name "hud".
  • Such object is present in a visible layer.
  • The scene with the name "hud" has been added as an overlay or underlay scene.
  • obj2 is not hidden.

Seeing the kind of error you've got it seems that the scene is there but the object isn't. You can print all the objects in the scene with print(hud.objects), that should help finding it. You can also print all inactive objects with print(hud.objectsInactive). If neither of them shows your object, it's probably hidden or unreachable by the game engine. You'll have to investigate what's wrong with it.

By the way, though it's true that you're defining a variable (and that is an object in Python), you are not defining an object (at last not in BGE). You're creating a varaiable that points to an already existing object, it would be more acurate to say that you are referencing an object. The same applies to scenes.

$\endgroup$
0
$\begingroup$

When you use this 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"]

maybe try changing...

if scene.name == "hud":

to...

else if scene.name == "hud":

Just a suggestion.

$\endgroup$
  • $\begingroup$ That doesn't matter, scene.name can't have two values at the same time and he is using diferent names for the variables, so they won't be overwritted. $\endgroup$ – elmeunick Jan 6 '16 at 11:43

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.