addObject objects not found suddenly

Hi all,

I’m making a game where I need some objects to be added to a scene many times.
I have the objects in a list in Python and add them thus:

myNumber = random.randint(0, 9)
newObject = scene.addObject(myList[myNumber], cont.owner, 0)
newObject.worldPosition = (x,y,0)

(this is within a for loop where x and y change each time, all in all it is run about 200 times and the list has 10 objects)

this works for a while and multiple of each object is spawned.

However, by the second iteration of the script it starts calling an error about not finding the list index.
I tried calling the objects without a list, and the same thing happened where it worked at first but then stopped being able to find the objects from the other layer.

Any ideas what could be wrong?

Are you certain that all of the names in your list exist in the scene on a hidden layer?

Yes, as I said all the objects in the list are added several times before it stops working so I know the list is calling the right things.
If I add a condition to only add objects if the list is not empty then it will add each multiple times and then stop, claiming the list is now empty. It is as if the objects in the inactive layer simply cease to exist.

Please provide a traceback and/or blendfile

Try: newObject = scene.addObject(scene.objectsInactive[objectName], cont.owner, 0)

From your description it sounds as the list is too short for the number you get. But I’m not sure - " error about not finding the list index" is not really a unique error as you have two lists this can happen (myList and the inactive objects).

I suggest you output some debug information to console:

  • number chosen
  • list size
  • list

Possible causes:

  • list is not what you expect
  • number is not what you expect
  • both are different at the different processing times.

Ok, so I couldn’t find what the problem was so I simplified the code and checked it when it was different.

it now does this:
newObject = scene.addObject(“myObject”, cont.owner, 0)
newObject.worldPosition = (x,y,0)

Where x and y are being incremented each time.

After adding the object a few times, it throws this error:

“requested name “myObject” did not match any KX_GameObject in this scene”

Is there a limit on how many times you can add an object?

No, there is no limit. It adds until you kill the process (which does not mean you can play anything :wink: ).

I guess you somehow ended the objects at the hidden layer.

Ok, I found the problem

“scene” was referring to “getCurrentScene”, but the current scene changed part way through meaning the inactive objects were no longer in “current scene”.
Thanks for all your help though.

Is there a limit on how many times you can add an object?

		 		 	  No, there is no limit. It adds until you kill the process (which does not mean you can play anything ;) ).

I guess you somehow ended the objects at the hidden layer.

current project