Script error

import bge

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

What is wrong with this script?

The console should be able to tell you what the issue is, but I’m guessing it’s the list comprehension you’ve got.


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

is easier to read in my opinion, and should work. Object.get() returns None if it ‘fails’ by default, so your original comprehension doesn’t satisfy a condition. You might be able to use ob.get(‘test_prop’) != None if you wanted.

EDIT: Also, you might not want to do that loop every frame, since the larger the scene.objects list gets, the more objects Python will have to look through to get the ones you specify. It’s more efficient to create a list and populate it with objects as they’re created (have them do it), and remove themselves when they’re going to be destroyed.

python test.blend (455 KB)I still get a error in the console.Here’s the blend.

Lol!A typing error, check console…

Attachments

python test.blend (459 KB)

It is not printing the objects with property gat though.

Man, your objects do not have this : ob_with_prop = [ob for ob in scene.objects if ‘gat_prop’ in ob]
The script looks for “gat_prop” , you have no objects that have “gat_prop” , remove “_prop” , and it will work.

So it is written wrong to detect gat property and print the cubes that have that property.

ya, your looking for a tag you did not place :slight_smile:

I found out what the problem was.The problem was i didn’t put gat_prop on the cubes.

oh really?
Have you even read my above posts?

Yes i did but didn’t get it at first.