Creating a list of objects with a certain property

Hi I have a question.

How would i find all of the objects in my scene with a certain property. Example is. I would like it to find all the bag objects in my scene and find all the ones with the empty true property. Any help appreciated

btw: I know its one of those i for i things. I just cant figure out how to make it work. Id like to learn more

Okay. A for-loop is a loop that allows you to loop through a list, like all objects in a scene, and perform an operation on each one of them. You can easily test to see if a property is in each object, and if so, append that object to a list, like so:



sce = logic.getCurrentScene()

objectlist = []

for o in sce.objects:

     if 'empty' in o: # If you find an object with an 'empty' property,
          
         objectlist.append(o) # Add the object reference to the list

print (objectlist) # Print out the list of objects that have an 'empty' property


You can easily add in a check to see what the ‘empty’ property is after the initial if check (i.e. if ‘empty’ in o and o[‘empty’] == 1).

EDIT: Note that this loops through every object in the scene, which is every game object in the current game scene. This will get slower with every object that you add. If you find that this list check is slow, then you’ll have to use a list you maintain yourself (i.e. a list of bags that you check).

I don’t know what you mean by “empty true property”. A property has a name, and a type.

So, assuming that you have objects with a boolean property called “bag”, you can get all the objects that have that property, with this list comprehension:


bags = [gobj for gobj in scene.objects if "bag" in gobj]

Only those where the property is set to true:

true_bags = [gobj for gobj in scene.objects if "bag" in gobj and gobj["bag"]]

Alternatively, code posted by SolarLune can be used to achieve similar goals.

@superflip - Goran’s code is more concise and easier to edit than my own, so I would go with his. In fact, normally I do use list comprehensions (what Goran suggested) rather than for loops when I need them. They’re very useful.

thanks for the help…is there a plce i can learn these particular things

Well, I have a tutorial series on my blog page at Game Up! Goran also has a series of video tutorials with using the BGE - you can find them in his signature.

You absorb programming by osmosis, meaning, if you see examples of feature “X” enough, you’ll gradually figure out how to use it.
These examples could be tutorials, asking questions, or looking at other people scripts.

Did you leave off something from the first bit of code it did not work for me Goran?

Goran’s code assumes you’ve already defined scene.

This should work out of the box, just change the string in “” from “test_prop” to your property name e.g “Enemy”


import bge

scene = bge.logic.getCurrentScene()

objects_with_property = [ob for ob in scene.objects if 'test_prop' in ob]

Or if you want to test if it has the property, and it is true, use this, again changing the string ‘test_prop’ to your property name


import bge

scene = bge.logic.getCurrentScene()

objects_with_property = [ob for ob in scene.objects if ob.get('test_prop')]

If i want to print the name of the objects out how do i do it?I want it to do something.


import bge

scene = bge.logic.getCurrentScene()

objects_with_property = [ob for ob in scene.objects if 'test_prop' in ob]

for p in objects_with_property:

     print (p.name)

Agoose’s first example with printing the name of each object.

Nothing is printing to the console of the blender game engine.

Are there any error messages? If not, then think about the script’s function; it finds all the objects in the scene with the property, then it stores them in a list. therefore, if there are no objects with the property, it doesn’t store them. It is likely there are two problems;

  1. You’re not activating the script with a sensor
  2. You’ve not changed the test property i’ve created. In the script change “test_prop” for “your property name”

Like agoose77 said, a script is complex, and so one rarely works straight out of the box with no customization.

Worked perfectly on mine


objects_with_property = [ob for ob in scene.objects if 'slot' in ob]
 
##Finds all objects with slot propery
 
true_used = [gobj for gobj in objects_with_property if "used" in gobj and gobj["used"]==True]
 
##Searches created list for a used property and sees if its true..creates another list of 
Used/True Slots
 
 
Thanks for the help guys :D
print (true_used)

Theyre are no errors in blenders console and it still doesnt print.I did everything you said.

Then you probably have an issue with your logic brick setup. Could you post a screen-print of it?

I have another quick question…I have my ‘bags’ named bag1, bag2, bag 3…but i dont understand the order it prints them in…when i run the script it returns “slot 3, slot 4, slot 2, slot1”. I was just wanting to learn how blender figures the order it puts the list in so i can better determine somethings place on the list.

How would you print sceenshot press prtscn sysrq.That doesnt work.

Upload a blend file then!