Append list of Class objects Based on Game Object

Hi!

I have got a bunch of objects in the scene named Row, Row.001, … Row.008

So I was doing something like this

row_obj_000= scene.objects['Row']
row_000 = RowOfAtoms.RowOfAtoms(row_obj_000)

row_obj_001= scene.objects['Row.001']
row_001 = RowOfAtoms.RowOfAtoms(row_obj_001)

....

and I was working with class RowOfAtoms objects row_000, row_001, …

Everything worked fine!

Then I decided to make a list of gameobjects and a list of RowOfAtoms class

objectlist = []
rowsofatoms = []

for o in sce.objects:

     if 'Row' in o.name: # If you find an object with starting with 'Row'
          
        
         objectlist.append(o) # Add the object reference to the list
         rowsofatoms.append(RowOfAtoms.RowOfAtoms(o))
         
         
print(objectlist) # Print out the list of objects  

print gives correct object list of [‘Row’, ‘Row.001’, … etc]

The problem is that this line
rowsofatoms.append(RowOfAtoms.RowOfAtoms(o))

gives errors. I mean I do not understand why appending the class instance based on the gameobject does not work!

Please, help!

Please format your code inside ```code``` tags, and explain the errors you are getting :slight_smile:

1 Like

tnx, I did not know how to format

Well, what are the errors?

1 Like

line with print(objectlist):

Blender Game Engine data has been freed, cannot use this python variable

However, if I comment

#rowsofatoms.append(RowOfAtoms.RowOfAtoms(o))

Everything work,I mean the list objectlist is correctly printed.

I guess I will try separate for

Nevermind!

Thx for the discussion tho

All I did I made another for loop

for obj in objectlist:
    rowsofatoms.append(RowOfAtoms.RowOfAtoms(obj))

and now no errors

I guess your class is something like

class RowOfAtom(bge.types.KX_GameObject):

    def __init__(self, object):
        ...

When you then wrap an object with this wrapper, the old gameobject reference will be destroyed.

Say you do:

a = scene.objects.get(...)
b = Wrapper(a)

# this will crash, because BGE freed the object stored in "a":
a.worldPosition

On the other hand, the object could also simply be removed, but the reference still exists inside your variables/lists, but they are marked as “invalid” and will raise exceptions when you try to use them.

Basically it is raising an error on the line with the print(list) because the references have been invalidated as I told you,

And because print will try to fetch the name of each object in the list, but BGE will say “nope, I freed that, you cannot get the name”.

2 Likes