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.
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.
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.
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;
You’re not activating the script with a sensor
You’ve not changed the test property i’ve created. In the script change “test_prop” for “your property name”
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)
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.