How Do You Add Overlay Scene with Python?

#1

I’m not very good with python and I’ve seen someone answer this in another thread but I didn’t understand it.

I just want a simple example like:

if my scene is ‘Overlay’

what is the exact command for adding the overlay without logic bricks? thanks

0 Likes

(Lyeb) #2

To add a scene named Overlay as an overlay,

bge.logic.addScene("Overlay") #so addScene(scene_name)

Note that the addScene method takes has a second, optional argument which determines whether the scene is added to the background or as an overlay – defaults to true, so if you don’t provide it with a value, the scene will be an overlay.

And to add a scene as background,

bge.logic.addScene("Assets",0) #addScene(scene_name,overlay=False)

Does that clear things up?
NINJAEDIT: also, check the API https://docs.blender.org/api/2.79/bge.logic.html#bge.logic.addScene

1 Like

#3

Thanks! That explanation is beautiful in its clarity and its simplicity!

0 Likes

#4

is there a way to remove the scene now with python? :sweat_smile:

0 Likes

(wkk.py) #5
scenes = logic.getSceneList()
scenes["name"].end()

Refer to the Python API documentation for the BGE

1 Like

#6

thank you but it tells me that there is only one scene [Scene] and it can’t find any others even though I have another hud scene and even an extra copy of it linked from another blend file (identical copy with same name)

0 Likes

(- Click for resources) #7

try this:


from bge import logic

def get_scene(scene_name):
    
    scenes = logic.getSceneList()      
    scene = None
         
    for sce in scenes:
        if sce.name == scene_name:
            scene = sce
    
    return scene

if get_scene('name_of_scene_here')
    #scene found, do something with the scene
    print('we found the scene')
else:
    print('Scene not found!')

and remember to find a scene the scene must be active (as main/overlay or undelay scene)

1 Like

#8

Thanks for all the rich info guys. I did some further digging and trial and error and it seems the structure of my code was wrong. You see, I didn’t know that it was important to mention that I was using modules rather than plain scripts.

0 Likes

#9

tee hee hee hee, this is what I ended up with. You see the whole “scenes[scenename].end()” never worked for me. Might have something to do with the fact that I’m using blender 2.64a.

So I had to write some functions and now it’s working without using logic bricks yippee!!

And shoutout to @Monster for the foundScenes part that I copied from one of his answers to another user! Though I used it for a slightly different purpose it was a nice technique - like I was just taught how to use the Rasengen or Kage Bunshin, thanks sensei

def add_this_scene_once(this_scene):
    
    scenes = bge.logic.getSceneList()
    
    foundScenes = [scene for scene in scenes if scene.name == this_scene]
    if not foundScenes:
        bge.logic.addScene(this_scene) 

def end_this_scene(this_scene):
    
    scenes = bge.logic.getSceneList()
    scenei = len(scenes)
    for scene in scenes:
        if scene.name == this_scene:
            scenes[scenei].end()
        scenei -= 1
0 Likes

#10

I just hve a question if I’m making a bunch of menus for a game, is it better to make each submenu have its own scene or to all of the submenu’s on the same scene and then toggle visibility?

0 Likes

#11

Okay, nevermind I figured it out. But can anyone remind me how to check a list to see whether he elements begin with a certain string like.

if I have

List = [apple, fanny, ape, fanatic]

how can I have python search for only the elemnts that begin with ‘ap’ - i saw it on a video but I can’t find it again thanks

0 Likes

(Lyeb) #12

Likely you’ll find answers to purely python, non BGE questions much faster if you google them out.
Anyhow,

#to see if individual string starts with a certain substring
"apple".startswith("ap")

#to check in a list, do list comprehension
new_list = [i for i in old_list if i.startswith("ap")]

Sidenote, it’s good practice not to use built-in type names such as “list” for variable names.

1 Like