Can't get scene by name although it's active

This basically is my script:


from bge import logic
cont = logic.getCurrentController()
own = cont.owner
scenes = logic.getSceneList()


for i in scenes:    if i.name == "targetingscene":
        targsce = i
    else:
        print("the shit is not there")




def trackto():
    targ = targsce.objects["cursor"]
    to_target = targ.worldPosition - own.worldPosition
    world_z = Vector((0, 0, 1*-1))
    crossed = world_z.cross(to_target)
    own.alignAxisToVect(crossed, 0)

I used to work before but it won’t this time. Am I blind? Am I not seeing anything?
The scene certainly is active and it’s given enough time to load. The script gets executed a second after adding the scene as an overlay. I even tried larger delays.

It will only tell me that the shit’s not there and that targsce has not been defined.
Do you see any reason or mistake I made?
There should definitely be a better way for getting scenes in later builds…

Thanks in advance, have a good night!
-Marv

The problem apparently was that the script gets executed by other controllers. I had to put the definition of the other scene inside the trackto function. The delay also is a problem. The other scene has to load before the script starts the first time. It still feels like it’s kinda random.

have the scene loading add a global , check for the global with other script?

Attachments

KeyDictandDoor(Commented).blend (579 KB)

Consider: why do you have to be able to track to something that hasn’t even been loaded yet? You probably don’t. You should have things set up to only run this stuff when you need them to.

For quite many things regarding input for example you can actually set to run only when there is input, for example linking it to keyboard/mouse sensors or input messages if you use them.

How about something like:


#assuming you only have 2 scenes total and you wait for the other to load
import bge

def overlaySceneLoaded():
    scenes = bge.logic.getSceneList()
    if len(scenes) > 1:
        return True
    else
        return False

def getTargetingScene():
    targetingScene = [s for s in scenes if s.name == "targetingScene"][0]
    return targetingScene

def trackTo():
    if overlaySceneLoaded():
        targetingScene = getTargetingScene()
        #the rest of your script

Use trackTo() as the entry point as that’s what you’re trying to do as far as I can tell. But I would first focus on minimizing the script execution before it needs to.