Hey there.
I’ve been quite frustrated trying to fix some code to get another scene (named hud_init).
At first i tried using this:
HUD = [scene for scene in scenes if scene.name == "hud_init"][0]
But i got a “List index out of range” error. I tried looking on Google for help but didn’t had luck.
Next thing i tried was this:
for b in scenes:
if b.name == "hud_init":
HUD = b
for i in scenes:
if i.name == "cursor_init":
cursorOverlay = i
moneyObject = HUD.objects['Money']
mouseCursor = cursorOverlay.objects['cursor_overlay']
But once i get to the moneyObject = HUD.objects[‘Money’] part it says that “name “HUD” is not defined” when it clearly is defined.
I’ve been on the brink to punch my monitor from the frustation, so if i could get some help here, i guess i will spare my monitor’s life :spin:
No, you’re only defining it if there’s a scene named “hud_init” in scenes. Since it doesn’t get defined there clearly isn’t a scene with this name in scenes.
I suspect that you’re adding the overlay scene during the same frame and expecting it to immediately show up in getSceneList(). As noted in the documentation, newly added scenes will not be available until the next logic cycle, so you need to wait one frame.
yes without seeing the top part of your script to see how your defining scenes theres no way to tell if there is an error in that part. Otherwise if you are defining it correctly it could be like mobious said. Mine is like that as well at this point. but it only throws the error once then it fixes itself after the hud loads.
hudscene = [scene for scene in logic.getSceneList() if scene.name == ‘hudscene’][0]
moneyobject = hudscene.objects[“my_object”]
thats how i define scenes. and objects in the overlay scene. You could always run a check before you access the object by checking if hudscene exists.
hudscenecheck = [scene for scene in logic.getSceneList() if scene.name == ‘hudscene’]
if len(hudscenecheck) > 0:
(tab)hudscene = hudscenecheck[0]
using function you can use “return” to stop immediately the code.
this is useful if the code “need” of something (dependence) and this thing is not available.
so, all check necessary can be done at the top of code then the code can run normally.
for example:
def main():
import bge
scenes = bge.logic.getSceneList()
try:
HUD = [i for i in scenes if i.name == "HUD"][0]
cursorOverlay = [i for i in scenes if i.name == "cursor_init"][0]
except:
return
moneyObject = HUD.objects['Money']
mouseCursor = cursorOverlay.objects['cursor_overlay']
main()
just to note that also these here potential errors:
The errors you get show there is no active scene named “hud_init” at the moment the code gets executed.
beside of that I recommend to encapsulate the code like this:
import bge
def findScene(name):
scenes = bge.logic.getSceneList()
foundScenes = [scene for scene in scenes if scene.name == name]
if not foundScenes:
raise Exception("Oh no! I'm missing scene '{}'. There are only these scenes {}"
.format(name, str(scenes)))
return foundScenes[0]