Prof. Monster's (Object) Finder

Hi,

Just for fun I created the module finder

What does it do?

  • It allows to search for game objects
    [LIST]

  • within one named scene

  • within all scenes

  • by any search critera

  • on any scene attribute (default = scene.objects)

  • It allows to define own search criterias (via matchers)

  • It returns a list of all matching objects (rather than a random object as "scene.objects[“Cube”] does.
    [/LIST]

Version:
1.0
2011-Dec-07

How to use?

  • import finder

  • call finder.findObjects(matcher, matcherParams, sceneToBeSearched, attribute)
    [LIST]

  • matcher: is a function that matches an object against search criterias

  • matcherParams contain the search criterias as list

  • sceneToBeSearch
    [LIST]

  • is the scene to look for objects

  • is the scene name of the scene to look for objects

  • “” to search the current scene

  • None to search all scenes

  • scene attribute to retrieve the objects from

  • None = scene.objects

  • any other attribute of the KX_Scene (objectsInactive, cameras, etc.)
    [/LIST]
    [/LIST]
    The result is ALWAYS a list of objects.
    The list is empty when no objects are found.

An LookupError will be raised if the provided scene(-name) is not active!

Examples:


import finder

...
# searches for objects with this string in name in all scenes
objects = finder.findObjects(finder.byNameContains, ["Cube"])

# searches for objects with this string in name in the current scene
objects = finder.findObjects(finder.byNameContains, ["Cube"], "")

# searches for inactive objects with this string in name
objects = finder.findObjects(finder.byNameContains, ["Cube"], None, "objectsInactive")

Available Matchers:

  • byName(obj, objectName)
  • byNameContains(obj, partialName)
  • byProperty(obj, propertyName)
  • byPropertyValue(obj, propertyName, propertyValue)

Let me know if you want more matchers included.

Custom matcher:
you can define your own matcher. A matcher is a function with following params:

  • the object to be matched
  • followed by the search criterias (can be several params)
  • returns True if the object matches otherwise False

def byMyCriteria(obj, numProperties, atLeastThisPropertyName):
  if len(obj.getPropertyNames()) != numProperties:
    return False
  if not atLeastThisPropertyName in obj:
    return False
  return True
...
#usage
myObjects = finder.findObjects(byMyCriteria, 3, "prop")
print (myObjects)

DemoFile
The Demofile is a 2.49 file and runs with all later versions (2.5, 2.6 …).
It contains finder.py. You can take it from there.

usage:

  • <space> press
    [LIST]

  • find objects

  • add a background scene

  • <space> release

  • find objects

[/LIST]

I hope you find that useful
Monster

Attachments

FinderDemo.blend (74.1 KB)


from bge import logic
own = logic.getCurrentController().owner
objects = logic.getCurrentScene().objects

enemies = [i for i in objects if 'enemy' in i]
enemies.sort(key=lambda o: own.getDistanceTo(o))
if targets : 
    myVar = targets[0]
    print( myVar )


this make a complete list ordered by distance (is also the version more short)
you can check the list adding this line at the bottom:


for i in targets:
    print(i, own.getDistanceTo(i))

similar but not identical…
this maybe is more easy to understand
only check the nearest enemy (not make a complete ordered list)


from bge import logic
own = logic.getCurrentController().owner
objects = logic.getCurrentScene().objects

enemies = [i for i in objects if 'enemy' in i]

minDist = 100
target = None
for i in targets:
    dist = own.getDistanceTo(i)
    if dist &lt; minDist :
        minDist = dist
        target = i
        
if target :
    myVar = target
    print( myVar )

:wink: