dealing with large groups of enemies

So, this isn’t so much a “this is broken” question as a “how would a professional do this” question. For a simple game I made recently (see attached file) I have rows of blocks falling that the player must stop. Everything is fully programmed and working, but this being my first project I have some questions about areas I think I could improve in. Mainly in dealing with groups or enemies (or in this case blocks). For my project I simply duped the code for each and gave them several states that could be triggered on and off that would determine how they interact with each other (the enemies code is in logic bricks, while the core game code is in python). My question is, in the future, how could I set up something that allowed me to spawn many instances of the same object like this without having to hard code the behavior of each? IE if a player specifies they want x amount of enemies, they are limited only by their CPU.

NOTE: I have been unable to upload my files, not sure yet if it is a website or a file issue. I will post it as soon as possible

Generally you should have the enemy object in an inactive layer and then just add as many of them as you need during run time. The exact implementation depends on how you want them added (e.g. all at once, over the duration of the level, added at specific spawn points, etc.), but generally you’ll use a for loop. If your enemies need to be initialized in some way, you can include the code for that in loop as well.

for i in range(numEnemies):
    # spawn the enemy
    newEnemy = scene.addObject('ObjName', spawner)

    # set anything you need to like position and AI behavior
    x, y, z = newEnemy.worldPosition
    newEnemy.worldPosition = [i*2 + x, y, z]
    newEnemy['AI'] = 'chase'

If you mean , large groups of animated characters , that’s complicated. In this example the rasterizer craps out at 40 animated objects. Maybe if you reduce the polycount in half you can get more on the screen. You have to fix the rasterizer or use a better vertex shader. I have no idea , but , If you want thousands of animated objects you have to do some very specific coding on the gpu. It’s not able to do it with normal , simple code.


animation 001.blend (253 KB)

thx for the help, i will keep working on the issue, but this is clearly not an easy fix!

I think if the items were using bullet and logic to animate
that the objects could “live” on the gpu, like PhysX etc.

Bullet 3.x will feature a 100% GPU accelerated rigid body pipeline with various parallel broad phase algorithms, convex and concave triangle mesh and several parallel solvers. Here is a teaser video for my GPU rigid body talk at GDC 2013 this Thursday March 28, 2.30PM. See some Youtube video here.