Frictional Games Forum (read-only)
limit number of dynamically created entities - Printable Version

+- Frictional Games Forum (read-only) (https://www.frictionalgames.com/forum)
+-- Forum: Amnesia: The Dark Descent (https://www.frictionalgames.com/forum/forum-6.html)
+--- Forum: Custom Stories, TCs & Mods - Development (https://www.frictionalgames.com/forum/forum-38.html)
+---- Forum: Development Support (https://www.frictionalgames.com/forum/forum-39.html)
+---- Thread: limit number of dynamically created entities (/thread-20273.html)

Pages: 1 2


limit number of dynamically created entities - darksky - 02-10-2013

hi
I am currently making a map where I create entities every x seconds. but after some time I get some performance problems with 600 entities^^. so I am now looking for a way to limit the number of entities which are active at the same time. Let's say the limit is 100.

the script without limit looks like this
Code:
int count = 0;
float timerInterval;

void spawnstone(string &in asTimer){
    count++;
    CreateEntityAtArea("stone_"+count, "stone_small01.ent", "SpawnArea_"+x+"_"+z, false);
   AddEntityCollideCallback("stone_"+count,"ShootArea_1", "setReady",false, 1);
    AddPropImpulse("stone_"+count, RandFloat(-2, 2), 0, RandFloat(-2, 2), "world");
    if (!(timerInterval<0)){
        AddTimer(asTimer,timerInterval,"spawnstone");
    }
}


I wanted to do something like that but then I realized there is no method "RemoveEntity"

Code:
int count = 0;
float timerInterval;
const int maxCount = 100; //  <------------

void spawnstone(string &in asTimer){
    count++;
    count = count % maxCount;            //  <------------
    if (GetEntityExists("stone_"+count)){//  <------------
    RemoveEntity("stone_"+count);    //  <------------
    }                                                //  <------------
    CreateEntityAtArea("stone_"+count, "stone_small01.ent", "SpawnArea_"+x+"_"+z, false);
   AddEntityCollideCallback("stone_"+count,"ShootArea_1", "setReady",false, 1);
    AddPropImpulse("stone_"+count, RandFloat(-2, 2), 0, RandFloat(-2, 2), "world");
    if (!(timerInterval<0)){
        AddTimer(asTimer,timerInterval,"spawnstone");
    }
}

is there an other way to do this?


RE: limit number of dynamically created entities - Kreekakon - 02-10-2013

You could set a local int that increases everytime you get a new stone, and cease stone production if it breaches the limit.


RE: limit number of dynamically created entities - NaxEla - 02-10-2013

Just make a looping function, then, everytime, check if 100 entities have been made (using local variables). If 100 have been made, use RemoveTimer(asTimer) to stop the function from looping.


RE: limit number of dynamically created entities - darksky - 02-10-2013

that's not what i meant Smile
i don't want to stop the timer. i want that the entities are continuously created (this is essential for my map because there are some other method calls in spawnstone that i did not mention), but only the newest 100 stones should be on the map.

i don't think I can simply deactivate the old entities because of name clashs
e.g
count = 101 % 100 = 1
setentityactive(stone_1,false) // the old stone
createentity(stone_1) // naming conflict, will get an other name
addcollidecallback(stone_1) // the old stone gets the callback

or am I mistaken in the handling of dublicat names?


RE: limit number of dynamically created entities - WALP - 02-10-2013

Is there not something about entities dissappear if you throw them into acid? you could try and look at how that acid works and create invisible acid. then make this acid activate whenever the number of rocks is too high.


RE: limit number of dynamically created entities - NaxEla - 02-10-2013

(02-10-2013, 06:25 PM)martinnord Wrote: Is there not something about entities dissappear if you throw them into acid? you could try and look at how that acid works and create invisible acid. then make this acid activate whenever the number of rocks is too high.

I'm pretty sure it just uses SetPropActiveAndFade


RE: limit number of dynamically created entities - Kreekakon - 02-10-2013

Well personally I'm not sure if setting something as inactive will remove a lot of the computer stress, but if it does here's another idea that you can use:

Still set another localint which calculates how many stones there are. When it exceeds 100, start ticking adding another localint which starts at 1(We'll call it "stonenum" for now)

Then you can do something like this everytime you add a stone:

SetEntityActive("stone_"+stonenum, false);
AddLocalVarInt("stonenum", 1);


The reasoning behind this should be simple enough. If you need more elaboration, I can explain it in more detail.

EDIT: This is untested script that I based off of another method I solved a problem of mine. May, or may not work.


RE: limit number of dynamically created entities - Apjjm - 02-10-2013

Make the entities breakable and use SetPropHealth to kill the entities. May i ask why you need to constantly spawn entities rather than cycle a bunch using ResetProp?


RE: limit number of dynamically created entities - darksky - 02-11-2013

Apjjm, that's actually a nice idea using resetProp and it is working. thank you.
I did not think about ResetProp because I thought it does not work on things which are created after the map is loaded because in the wiki it says
"Resets a prop's state to the original one when the map was loaded."

the working code now looks like this:
Code:
int stonecount = 0;
float timerInterval = 2;
const int maxStones = 100;

void spawnstone(string &in asTimer){
    stonecount++;
    stonecount = stonecount % maxStones;
    if (GetEntityExists("stone_"+stonecount)){
        ResetProp("stone_"+stonecount);
    }else{
        CreateEntityAtArea("stone_"+stonecount, "stone_small01.ent", "SpawnArea_"+x+"_"+z, false);
    }    
    AddPropImpulse("stone_"+stonecount, RandFloat(-2, 2), 0, RandFloat(-2, 2), "world");
    if (!(timerInterval<0)){
        AddTimer(asTimer,timerInterval,"spawnstone");
    }
}

fyi, I track the player position using scriptareas and let the stones spawn above the player and the stones will cause damage if they hit the player.


RE: limit number of dynamically created entities - palistov - 02-12-2013

There's a couple things you could try to "move" entities without destroying them. Mind you I've never tried any of these, so it will take some experimentation on your part, these are just suggestions.

1) Sticky areas. After you've created 50 rocks, start recycling them from 1 to 50 using AttachPropToStickyArea and DetachFromStickyArea
2) Create "dummy"entities instead of script areas and use AddAttachedPropToProp and RemoveAttachedPropFromProp (this might do you well, I made a spider follow a player around like a pet using these. It was kinda cute)