I’m working on a simple project in the blender game engine for a digital arts class. The primary goal was to use an Arduino microcontroller to control objects in the BGE, and I’ve accomplished that. I hope to clean up the code and submit it to the blender community after finals are over.
As part of the game, I’m randomly producing a number of object instances using the addObject command in a python script. Unfortunately, due to the nature of the game, there’s not much acting to get rid of them, which makes the sim get pretty sluggish after a while. I can use the time setting to have them delete themselves, but as the primary prop in the game, it is a bit odd to see them just suddenly pop out of existence. So, could anyone help me out with the script I would need to count the number of instances present already? That way, I could just tell the script to not produce any more until the number dropped again.
If you want them to fade out slowly rather than just popping out of existance, you can use an IPO actuator with the OB color IPO (alpha) to get them to fade from view.
objcount=0
objName = "your object name" #add OB before the name if you are using 2.49
for obj in bge.logic.getCurrentScene().objects:
if obj.name == objName.name:
objcount++
print objcount
Monster, i know that my script do the same thing as yours, but can you explain how to use this kind of syntax?
obj for obj in GameLogic.getCurrentScene().objects if obj.name == objectName
I know it return a list(since you use len on this) and it should contain the objects with that name(since in your script len(list) return the number of objects with the given name in the scene), but i can’t get how it works… I’ve seen it many other time, but never find anything about how to use it
Fantastic! This is exactly what I needed! Not only does it do what I wanted it to, it also helped me debug my other method for removing the objects (endObject when more than 3000 units away) which I thought was already working (but it wasn’t :D). This len() function seems pretty useful as well…