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")
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].
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) <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.
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) < 100:
g.enemyList.append(obj.name)