1
$\begingroup$

I have a text object in a scene. That scene is added at the start of the game as an overlay scene. Whenever I try to access the text object, named "HUD.text" (to change the game text) the bge returns:

Traceback (most recent call last): File "path\to\my\script.py", line 71, in main HUDtext = hud.objects['HUD.text'] KeyError: "CList[key]: ''HUD.text'' key not in list"

Which if I'm understanding it right means the object 'HUD.text' is not available for the game engine.

I have checked that the overlay scene is being added correctly (it is), and if I print all the objects in that scene 'HUD.text' is in the array.

I add the overlay scene with this line in a script that runs once at game start. bge.logic.addScene('hud', 1)

hud is defined by hud = bge.logic.getSceneList()[-1]

print(hud.name) outputs hud

print(hud.objects) outputs [Plane, HUD.text, Camera.001]

Is there something I have to do to make objects added in a overlay scene available? Or is there some huge delay in the time it takes to add the scene?

Here is an example blend file showing the error.

$\endgroup$
  • $\begingroup$ I can't see a problem. I suggest to create a demo.blend and post it. Remark: while this is an interesting problem, changing text goes very well with messages ;). $\endgroup$ – Monster Aug 19 '15 at 6:42
0
$\begingroup$

Your file works for me in 2.78, after pressing Space I see text saying "123".

I still get the error the first time hud.objects is accessed, on the same tic as the scene is added. The documentation explains:

This function is not effective immediately, the scene is queued and added on the next logic cycle where it will be available from getSceneList.

Emphasis mine

To avoid this error, you could do something like:

def main(cont):
    own = cont.owner

    if "scene_init" not in own:
        own["scene_init"] = True
        bge.logic.addScene('hud',1)
    else:
        hud = bge.logic.getSceneList()[-1]
        HUDtext = hud.objects['HUD.text']
        print(HUDtext.text)
        HUDtext['Text'] = '123'
$\endgroup$
0
$\begingroup$

You can not use a variable for KX_GameObject that contains characters like ".", ",", "!" etc. You must use only lowercase and uppercase letters and numbers. In this case you must do something like:

HUDtext = hud.ojects["HUD.text"]

or

HUD_text = hud.objects["HUD.text"]

You can use any variable name that contains only letters and numbers. Note that it can't start with number.

$\endgroup$
  • $\begingroup$ sorry that is not the issue at all. I changed the var name when copying in to my question, not thinking that it was invalid. $\endgroup$ – David Aug 18 '15 at 4:05
  • $\begingroup$ Actually I doubt if overlay scene objects are aviable, but I really don't know it - haven't used yet. If they are aviable, than gamesystem classes or glbalDict are not needed to be updated every frame, just when switching scenes. $\endgroup$ – Adrians Netlis Aug 18 '15 at 4:54
  • 1
    $\begingroup$ At least now, the above variable names are correct and the error belongs to the dictionary access (not variable declaration). $\endgroup$ – Monster Aug 18 '15 at 5:45

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.