Ammo Counter Display

I am setting up an ammo counter display for a game that I am working on. I am trying to set up a python script that:

  1. Gets a list of all of the objects in a scene.

  2. Finds all of the objects on that list that have a int property named “Ammo”.

  3. Finds the value of each ammo property (the objects all have a property named
    “Ammo” but the value is different for each).

  4. Finds if the value each property is within a range, and if it is, sets the object that has that property visible.

Here is my code so far (which doesn’t work):


import bge

def main():

    cont = bge.logic.getCurrentController()
    # Gets list of scenes
    scelist = bge.logic.getSceneList()
    sceHUD = [s for s in scelist if s.name == 'HUD'][0]
    # Gets object list for HUD scene
    objectlist = bge.logic.sceHUD.getObjectList()

    for object in objectlist:

        # Finds objects with property "Ammo"
        if hasattr(object, "Ammo"):
            AmmoCount = Ammo.value

            # Checks value of each "Ammo" property and if it is within a range, sets the
                object holding it visible    
            if AmmoCount <= 5 and >= 0:
                 object.visible = True
            else:
                  object.visible = False

main()

Can anyone debug this code or show me what i’m doing wrong?

Thanks

I have done some more work on the code but it still doesn’t work. Here is what i’ve got:


import bge

def main():

    cont = bge.logic.getCurrentController()
    sce = bge.logic.getCurrentScene()
    ob_list = sce.objects

    for ob in ob_list: 
        if ob.has_key('Ammo') == True:
            texPos = ob['Ammo']
       
        if texPos < 5 and texPos >= 0:
            ob.visible = True
            
        else:
            ob.visible = False
main()

I’m not sure, but I think the 2.5 way is:

if “Ammo” in ob:

I confirm that LaH is correct.

Instead of “if ob.has_key(‘Ammo’) == True:”?

import bge

def main():

    cont = bge.logic.getCurrentController()
    sce = bge.logic.getCurrentScene()
    ob_list = sce.objects

    for ob in ob_list: 
        if not "Ammo" in ob:
            continue
        texPos = ob["Ammo"]
        if texPos < 5 and texPos >= 0:
            ob.visible = True
            
        else:
            ob.visible = False
main()

Or, even faster:

import bge

def main():

    cont = bge.logic.getCurrentController()
    sce = bge.logic.getCurrentScene()
    ob_list = sce.objects

    for ob in [i for i in ob_list if "Ammo" in i]
        texPos = ob["Ammo"]
        ob.visible = texPos < 5 and texPos >= 0:

main()

Good idea, i was too lazy to do so!

Ok, thanks, would i need to put the

“else:
ob.visible = False”

outside of the for loop, or indented under it, or did you leave that out for a reason?

Nope. His statement essentially returns that value to the visible function

The expression: texPos < 5 and texPos >= 0 is boolean (True or False) so I just assign it to ob.visible that is a boolean (True/False) property. No if statement needed at all (witch saves some execution time).

Exactly! It does however make confusion for beginners*

Okay, I get it now, Thanks for your help.