darksky
Member
Posts: 52
Threads: 8
Joined: Nov 2012
Reputation:
2
|
limit number of dynamically created entities
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
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"
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?
|
|
02-10-2013, 05:24 PM |
|
Kreekakon
Pick a god and pray!
Posts: 3,063
Threads: 70
Joined: Mar 2012
Reputation:
124
|
RE: limit number of dynamically created entities
You could set a local int that increases everytime you get a new stone, and cease stone production if it breaches the limit.
|
|
02-10-2013, 05:42 PM |
|
NaxEla
Senior Member
Posts: 415
Threads: 5
Joined: Dec 2012
Reputation:
28
|
RE: limit number of dynamically created entities
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.
|
|
02-10-2013, 05:53 PM |
|
darksky
Member
Posts: 52
Threads: 8
Joined: Nov 2012
Reputation:
2
|
RE: limit number of dynamically created entities
that's not what i meant
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?
|
|
02-10-2013, 06:19 PM |
|
WALP
Posting Freak
Posts: 1,221
Threads: 34
Joined: Aug 2012
Reputation:
45
|
RE: limit number of dynamically created entities
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.
|
|
02-10-2013, 06:25 PM |
|
NaxEla
Senior Member
Posts: 415
Threads: 5
Joined: Dec 2012
Reputation:
28
|
RE: limit number of dynamically created entities
(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
|
|
02-10-2013, 06:27 PM |
|
Kreekakon
Pick a god and pray!
Posts: 3,063
Threads: 70
Joined: Mar 2012
Reputation:
124
|
RE: limit number of dynamically created entities
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.
(This post was last modified: 02-10-2013, 06:41 PM by Kreekakon.)
|
|
02-10-2013, 06:39 PM |
|
Apjjm
Is easy to say
Posts: 496
Threads: 18
Joined: Apr 2011
Reputation:
52
|
RE: limit number of dynamically created entities
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?
(This post was last modified: 02-10-2013, 11:03 PM by Apjjm.)
|
|
02-10-2013, 11:02 PM |
|
darksky
Member
Posts: 52
Threads: 8
Joined: Nov 2012
Reputation:
2
|
RE: limit number of dynamically created entities
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:
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.
|
|
02-11-2013, 11:13 PM |
|
palistov
Posting Freak
Posts: 1,208
Threads: 67
Joined: Mar 2011
Reputation:
57
|
RE: limit number of dynamically created entities
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)
|
|
02-12-2013, 09:05 PM |
|
|