How can I set a property of a certain added object?

I have this script:

for ob in own['Stored'][point[1]]:
   added = own.scene.addObject(ob[0],own.scene.objects['Empty'],0)
   added.worldPosition = ob[1]
   for obj in added:
      if "Text" in added:
         added['Text'] = ob[2]

I have all the values written in a list and I want to access the number 2 value but only if the added object has the property Text.
How can I run a loop for objects in an added object?
This gives me an error: TypeError: 'KX_GameObject' object is not iterable

Try that:

You’re looking for the “children” list.

Thank you for your suggestion but that doesn’t seem to be the right answer.
the ‘added’ object represents all the objects in the list and not it’s children.
I would like to somehow differentiate the objects from the list if they had a certain property.
For example, the list looks like this:

[( 'Cube', (x, 3 ,0), property1), ( 'Text', (x, y, 0), property2)]

The first value is the object, the second is its location and the third is its property.
How can I access for example, if the added object has a property named ‘property2’?

Not sure I understand the question correctly, but I think the main principle can be extracted from this file?

from bge import logic

currentScene = logic.getCurrentScene()

sceneObjects = currentScene.objects

propertyNameTarget = "virus"

for ob in sceneObjects:
    if propertyNameTarget in ob.getPropertyNames():
        print("KX_GameObject: > " + ob.name + " < has property: > " + propertyNameTarget + " <")

FindObjectWithSpecificProperty_01.blend (535.1 KB)

In your case the code would need to be slightly different, but from what I can see I think you can figure the rest out yourself ; ) If not let me know and I’ll try to help.

Edit: Removed an unnecessary line, and reuploaded test file.
Edit2: Clarity.

1 Like
for obj in added:

Added is the object that has been spawned, it’s not a list, as the error says you can’t loop trough the object.

so remove that line.

i assume

own['Stored']

is your list.

so

for data in own['Stored']:
    if 'Text' in data[2]:
        added = own.scene.addObject(data[0], data[1], 0)

incase it throws an error on the added part split the location to

added = own.scene.addObject(data[0], None, 0)
added.worldPosition = data[1]
1 Like

Thank you for your suggestion.
But basically, I would like to do just this:

if "Text" in added:
    added['Text'] = ob[2]

Just to check if the added has the property"Text" and do something if it does. But I don’t know how to write it correctly.

then remove this line:

for obj in added:

Oh, I see, that would work?
I can’t test it now and I wasn’t sure if it would work like that.

yes, because…

added is already the added object, so the only thing left to do is check if added holds the property, if so, then simply change the text.

Ok, thank you.
I didn’t know if I’ve written it correctly.
For some reason, I thought that I needed another loop for objects to determine if the object has the property or not. But that shouldn’t be the case because there is already a loop for an object at the beginning of the code and a simple check afterward if the property is in the added object should be enough.

Is this “Empty” the object with the “Text” property? Or is it something parented to your empty?

The empty is a spawner, position where to add a new object