I need to create a huge amount of entities, but i have to remove a lot of them at the same time, and that destroy the FPS
Caching is the key. The idea is to hold a cache of entities and to mark if they are active (visible objects acting in the world) or inactive (objects that are currently not acting and therefore invisible and passable). So, if you create a new entity, you first look into the cache if you already created an entity of that type and you just "switch" it on.
Following is some pseudo code which I took from a project in which I needed such a system.
First, the create function. The important thing is, that you don't create the entity with the desired entity action. You store the entity action in one skill, so that a "watchdog" function executes the function in a separate while loop and not the entity alone. This is by the way faster than the ususal per-entity-while(1)-wait(1) approach.
ENTITY* ent_createcached (char* filename, VECTOR* position, void* ev)
{
// search if there is a free entity for the given filename in the cache
if no entity found in cache
{
// create entity with NO event
// make invisible and passable
// put entity in cache
}
else // reset cached entity at desired position
{
// set position
// set angle to 0
// set scale to 1
// reset skills
// reset INVISIBLE | PASSABLE | TRANSLUCENT
}
// assign event to a skill
// mark entity as active
// return entity
}
Second thing is the manager function, which is started once (e.g. at startup) and runs infinitely. It runs through the cache and executes for each active entity the stored function. It switches the my pointer to the entity, so that you can use the my pointer in your event:
void ent_cache_startup ()
{
// reset cache
while (1)
{
// go through cache
for (...)
{
if entity is active and has event stored in skill
{
// set my pointer to entity
// load event function into function pointer
// execute event
}
}
wait(1);
}
}
Third, the remove function. You simple search the entity in your cache and mark it as "inactive" and make it invisible, passable and static.
BOOL ent_removecached (ENTITY* e)
{
// set entity in cache to inactive
// make invisible and passable
// make static and solid
// set parent entity to NULL
// set event in skill to 0
}
Please note, that in my pseudo-code, I assume a cache that caches the entities by filename. That is, if you have e.g. 10 items of "a.mdl" in your cache and all of them are active, you create a new entity. You could also do it that way, that you simply morph an inactive entity.