Count nearby objects with specific property

Hi, I’ve been looking for a python script that would use a near sensor to detect nearby objects with property “near”. Then I want to count the amount of objects that trigger the near sensor. So far I’ve been able to gather a list of the nearby objects, but I can’t figure out how to count them.

Setup: 3 cubes surround another cube (cube 1) and they contain property “near”. Cube 1 should tell me that it detects 3 cubes nearby. If there are less then 2 cubes nearby cube 1 should be set invisible.

current code:


import bge


def main():

    cont = bge.logic.getCurrentController()
    own = cont.owner

    near = cont.sensors['Near']
    actu = cont.actuators['Act']
    
    points = 0
    
    if near.positive:
        for i in near.hitObjectList:
            list = [i]
            print (list)
            
            
    

main()

Thanks for any help!

len(near.hitObjectList) # Get the length of the list

Wow. Thanks a ton, that work great!