simple scene handling issue

I am getting a list error, that the scene I am trying to access does not exist(CList[key]: key not in list)…we’ve all seen(no pun intended) it before and it is usually something simple…but I cannot figure out why I am fighting with this…I even went so far as to copy and pase code I had that worked before, rewriting the script from scratch I still get the error…

I am thinking maybe something I am doing is just a simple mistake


scenes = bge.logic.getSceneList()
ui_scene = scenes['ui_play']

I have also tried accessing the index


scenes = bge.logic.getSceneList()
ui_scene = scenes[1]

I still get the error…I can print the list and it shows up in the console, so I know the naming is correct and that it does exist…so any thoughts?

are you adding the scene at game start?

it takes 1 frame to add the scene, so everytime i start my game i have 5 messages that there is no scene “Overlay”, but the next frame its there and the error gone

(solution i have for this is that i have a start scene, which adds the overlay scene and the normal scene and deletes itself, so the overlay and the normal scene are both simultaneously added and there is no error )

so I should just have a simple if the scene is loaded statement, then continue…?

EDIT:

so I tried a simple for loop and I no longer get the error, but the print statement also returns nothing…?


for ui in bge.logic.getSceneList():
    if 'ui_play' in ui:
        print(ui)

if I add the print function directly after the loop it returns ‘world’, ‘ui_play’


for ui in bge.logic.getSceneList():
    print(ui)
    if 'ui_play' in ui:


# to grab an object, get_object('scene_name','object')
# to grab a scene, get_scene('scene_name')


def get_scene(scene_name):
    
    scenes = logic.getSceneList()      
    scene = None
         
    for sce in scenes:
        if sce.name == scene_name:
            scene = sce
    
    if scene == None:     
        print('
*** No scene found with the name: ' + str(scene_name) + ' ***
')
    else:
        return scene
    


def get_object(scene_name, object_name):
      
    scene   = get_scene(scene_name)
    object  = scene.objects[object_name]
    
    return object

make sure the scenes are running.
The above code will check if the scene exists and if so it will return it.

thanks again cotaks, I did get it…I basically just let it ‘wait()’ until the game recognizes the scene…it was as Leralasss stated…it took a frame to load it.

are you using module mode?
because it should not be a problem to grab a scene at start, it always worked for me.

#edit
another thing you can do is set the logic brick of the add scene brick to let it run as first.

The Python controller mode is irrelevant.

It is (as mentioned above) a question of timing. The scene list is - the list of the currently loaded scenes. At game start only the start-up scene (the current one) is loaded. Therefore you can’t find the other scene.

This means you better run that code when the requested scene is loaded too. Which is earliest the frame after loading the requested scene.