How can I verify if an object is actually in the scene.

Hi, I can’t figure how to verify if an object is in the scene or not.

Here is what I have:

-The game create the letter ‘A’ as object a couple times during the game.
-When you press the ‘a’ key it deletes the ‘A’ object and plays a sound.

simple right!!!

But here is my problem :
The sound occurs if the key is down.

I would like the sound to occur ONLY if t
1:he ‘A’ object is in the scene,
than
2:if the key is down.

I found this line of code maybe it is of some use:

scene = bge.logic.getCurrentScene()

I tryed to use:

 if 'A' in scene:

But it doesnt seems to work :S

If anyone has an answer id be really happy !

And if you do maybe you want a bonus questions for extra points !
(Is it possible to say: if A is in scene more than NUMBER of times)

Thancks in any case.


def getObjectsByName(name, scene):
    return [object for object in scene.objects 
            If object.name == name]

To know if an object with a name exist:


    foundObjects = getObjectsByName("A", bge.logic.getCurrentScene())
    If foundObjects:
      print("The scene {} has {} objects with the name {}".format(
            foundObjects[0].scene, len(foundObjects), foundObjects[0].name))
    else:
        print("No object with such a name")

Awesome thx !

The KX_GameObject have an attribute for get the scene in which it is stored, I think it would be a much easier way, example:


if object.scene.name == bge.logic.getCurrentScene().name:
    print ("The oject is in this scene")

@carlo697

You can just compare the scenes directly: “obj.scene == current_scene”

Oh, i forget that, sometimes i simply answer fast and I forget some details, anyway Thank you!