limiting amount of objects in scene

I know how to do this for one object, to limit the amount that can enter scene but not for many different objects.

Example. Lets say I have 5 different objects that can enter scene but i want the TOTAL
number of objects in scene , 7 total in scene no more than that, so let’s say i have 4 of object1 and 2 of object2 and 1 of object3 in scene. If i want another object i would have to delete one of the objects in scene to add another object not sure how to set that up 2.49

digiman

There are many ways you can do it.

One easy way,
You can get the object list through python and take out of the essential objects from the list (e.g. Camera, Lights). Then every tick you check the new object list’s length (using the len function). If the object list passes a certain number, you can use pop() (to take out the last object on the list) and end it.

You could slice the object list at a certain length. If any objects are returned in that slice, end them. Something like this:


scene = bge.logic.getCurrentScene()

length = 10

overflow = scene.objects[:length]

for object in overflow:
    object.endObject()

That was exactly what I was suggesting haha

The reason I didn’t use the full object list was because it may delete the camera, or lights, or things he may not want to delete.

Or are those always first on the list anyways?

can it be done with logic brick?

digiman

Yes and no. If you control the number of objects entering the scene, then simply have an object that keeps count of what has been added. Then, you can determine if it can be added.

There are two main ways of doing this:

  1. Global counter, which acts like a parent:

It uses messages to increase or decrease a property when an object is added / ended. If there are too many objects, it doesn’t say YES to the add request. You can use a request property to determine if that object should add the object. (see example)
Message1.blend (423 KB)

  1. Global counter, which adds the object:
    Same as before, but performing the logic.

I am definitely not used to the newer BGE but your example shows only one type of object so i am still unsure how to connect for more than one object. I will go back and look at the blend see if i can add a cube and other objects and keep them limited when entering scene thanks