[Python] Using lists

Hey. I tried to modify the code shown in this thread:
http://blenderartists.org/forum/showthread.php?t=153065
to filter through the list of children of an object and to generate a list of the ones that have the property “Spawner”, and then assign the global variable “spawner” the first object in this list.

However, when I run it I get an error message saying that in the line underlined and in bold below ‘str’ object has no attribute ‘name’. How can I work around this?

import GameLogic as g
cont=GameLogic.getCurrentController()
own=cont.getOwner()

#List the children
childList=own.getChildren()
#create the spawner list
spawnerlist = []
#Filter out non-Spawners
for obj in childList:
    if hasattr(obj, "Spawner"):
        spawnerlist.append((obj.name))
        firstSpawner = spawnerlist[0]
        <b><i>spawner = firstSpawner.name</i></b>
        print ("test")

Many thanks

It looks like you are adding the object’s name to your list. So your list is of names, which don’t have names of their own. try:

import GameLogic as g
cont=GameLogic.getCurrentController()
own=cont.getOwner()

#List the children
childList=own.getChildren()
#create the spawner list
spawnerlist = []
#Filter out non-Spawners
for obj in childList:
    if hasattr(obj, "Spawner"):
        spawnerlist.append((<i><b>obj</b></i>))
        firstSpawner = spawnerlist[0]
        spawner = firstSpawner.name
        print ("test")

…if spawner is your global variable, you might be able to save it directly, rather than by name. That is, spawner = spawnerList[0].

Thanks Zaerin, I understand now.

I have a different problem now though!

I found a “track to nearest enemy” script on this site thanks to Skullbunny, at http://blenderartists.org/forum/showthread.php?t=90813&highlight=track+nearest+enemy . I think can get this working in my project, but I’d like to understand what it does. Could someone please explain what’s happening here:

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
scene = g.getCurrentScene()
objList = scene.getObjectList()

#create a list of all enemies within range 100
#change this number to best fit your game area
g.enemyList=[]
for i in range(0,len(objList)):
    obj = objList[i]
    if hasattr(obj,"enemy"):
        if own.getDistanceTo(obj) &lt;100:
            g.enemyList.append((obj.name))

The part I don’t understand is the line “for i in range(0,len(objList)):”

I don’t know where the ‘i’ was defined and what range(0,len does.

Thanks again

i don’t like that code, lists should be walked using for directly.

this is how i would do it (without using more advanced programming):

import GameLogic as g
cont = g.getCurrentController()
own = cont.getOwner()
scene = g.getCurrentScene()
objList = scene.getObjectList()

#create a list of all enemies within range 100
#change this number to best fit your game area
g.enemyList=[]
for obj in objList:
    if hasattr(obj,"enemy") and own.getDistanceTo(obj) &lt; 100:
            g.enemyList.append(obj.name)

Thanks cyborg_ar! :smiley: That one actually makes sense to me and I’ll probably use that version in my project now, but I’m still curious as to why they used

for i in range(0,len(objList)):
    obj = objList[i]

in the first place and how it works

edit: I understand that now.

If I have an object name as the target of a track to Actuator, how can I get the object of that name?