Script problem in spawned objects


I’m trying some kind of LOD system with zombies. I have a animated zombie model, that is spawned from another layer. The idea is to have a huge amount of zombies ( one single zombie does not scare anyone :eyebrowlift2: )

But to have a lot of armatures in the engine, slow down the game, so my idea was to replace de 3d model for a plane with a animated texture when the zombie is far from the camera, so in the distance you’d see a lot of zombies walking to you. This is how old school game, like doom worked.

The animated texture is controller by a script, and everything works fine, I mean I can have a lot of animated planes with a good framerate.

I’m using “end object” “add object” to change between the animated plane and the 3d model when is near.

The problem is…

When “add object” is used to add a plane with the animated texture (controlled by a script), it works, but if I add a second plane, the animation start to not working well. It looks like the script is running from both to both planes and the animatión is not finishing before to start again. You can see how it is worse with 3, 4 …

I think the problem is that all the planes added by the “add object” have the same “controller.owner” and that’s why all the scripts are running at the same time for all the planes. And I don’t know how to solve this problem.

Maybe a solution is use the texture like a normal animated texutre, without the script, but I would like to know how to solve the problem with the script and added objects.

Thanks in advance.

P.D. blend file:
Push space to add zombies and “s” to go farther and get the LOD system start to work.

It is most likely because the planes share the same mesh.
Meaning your uv script’s offset “uv[0] += 1.6667” is actually “uv[0] += 1.6667 * number_of_planes_spawned”
Each of the planes changes the uv every frame.

Will look into it.
Otherwise, damn, that is quite well done. :slight_smile:
Certainly something i would want to do/have at some point.

EDIT: yeah, it is because of that.
Ran the uv script only on one (reference) flat zombie. and it worked, but everyone was dancing the same moves synchronized.
LibNew should be the solution.

“”"You may add an object with relatively unique mesh using LibNew.

idx = 0
New_mesh = bge.logic.LibNew('Model'+str(idx),'Mesh', ['Mesh_Name_here'][0] 

You need to add an object first and then replace its mesh using LibNew. The reference Mesh needs to be in a hidden layer.
The Library name needs to be unique, that’s why I use the idx variable to alter a static name!"""

Thanks, it works like a charm.

Here it is the finished blend:

Push space to get a new wave of zombies :yes: wasd to move.


Small tip:
As you plan to spawn a lot of zombies,
you only need as many unique meshes as there are frames (3 in your case?).
You don’t need 400 unique meshes for animations that can be ran with 3 reference meshes.

Also, it is critical that you don’t run (the same) script on hundreds of zombies,
if it can be done on one empty with a while / for loop.
The launch of script is quite resource heavy.

Thanks for the tip, I’ll try it.