I have a problem with optimization of my code. Right now I have a few hundred enemies that all actively search for targets to attack. The way I am doing this is I have placed a timer in Update() which counts down. This results in the enemy checking for targets every second or so. However, this means I also have a few hundred floats changing, which I think is expensive. I have read about using coroutines to balance the load better, but I haven’t really found a good way. Below is an example of the code I have:
function Update()
{
//select a target for the targetlist
if(targetListAI.Count != 0)
{
if(checkTimer > 0.3f + (0.05f * troopNumber)) //each enemy has a number from 1 to 100 assigned to them. I used this to make sure they are not all called at the same time.
{
if(currentTarget != null)
{
if(currentTarget.CompareTag("Soldier"))
{
var statusSoldier = currentTarget.GetComponent(troopScript).getStatus();
if(statusSoldier == Status.Dead)
{
currentTarget = null;
}
}
}
//Try to find a target or a better target
if(monster.attack == attackType.Melee)
{
SelectTargetMelee();
}
else
{
SelectTargetRanged();
}
checkTimer = 0;
}
checkTimer = checkTimer + Time.deltaTime;
}
}
Anyone have any suggestion on how I should optimize this?
I tried using InvokeRepeating(), but that results in a lower framerate.
function Start()
{
var repeatTimer : float = parseFloat(troopNumber) / 100.0f;
InvokeRepeating("SelectTargetAI", repeatTimer * 2, 1);
}
function SelectTargetAI()
{
//select a target //the AI will not be constantly looking for new targets, but will rather focus on killing their current target or their current attackers
if(targetListAI.Count != 0 monster.attack != attackType.Null !taunted)
{
if(currentTarget != null)
{
if(currentTarget.CompareTag("Soldier"))
{
var statusSoldier = currentTarget.GetComponent(troopScript).getStatus();
if(statusSoldier == Status.Dead)
{
currentTarget = null;
}
}
}
//select a new target even if there is already a target
if(monster.attack == attackType.Melee)
{
SelectTargetMelee();
}
else
{
SelectTargetRanged();
}
}
}
That’s a lot of enemies for any game. Really depends too on how high the poly count is or your computer. I would suggest either spawning less enemies or disabling enemies that are further away. One thing I’ve noticed though is if there’s a lot of code being called in the Update function that you’re game will chug.
I guess maybe a little more info. Does the game chug only with a lot of enemies present?
If the enemy count is below 100, it works fine, but I have seen threads from people complaining about 2 ms in the main thread, so I am not sure what I should be aiming for. What is normal for PC game?
Hmmm I am not sure about the ms. I would say it could just be all the pieces of code being called or the amount of drawcalls. As long as you have high Fps it should be fine or you may have to change a few things. If you have more then 100 enemies including offscreen then that may be an issue. If the enemies are in groups then I would create a Single Empty GameObject and attach a wander script to that. If it detects that the player is in range, then enable all the children of that node ( enemies ). That way all those scripts wont constantly be called. Then if the player gets out of range disable all the enemies again.
I am already doing that. I think it is the code, because FPS fluctuates between 70 and 17 before and during large battles.I’m going to edit out pieces of code to see where the trouble lies.