Handling tens to hundreds of enemy AI

The gameplay loop requires that there be a very vast amount of enemy AI for the players to encounter.


The only issue in my thought process is how I would not only handle everything (AI script + Statemachines and other things like animations and positions) but also optimisation of the enemy AI too.

I was thinking of handling the AI all on the client or at least model positions/models and/or animations but that could cause issues on the client and server let alone replication too.


If anyone has had to face this before or has at least dealt with a vast amount of enemies within their game do let me know your thoughts, ways you’ve approached this issue or other useful information.


Useful information:

1 Like

i made the ai hitboxes on the server and the client renders them with fake characters where the humanoid is replaced with just an animation controller

2 Likes

My question to this is how you’re handling the entity on the server. I have seen (or heard of anyway) people using a part to replicate the position of the entity.

Now if I do this I am not too sure how I would handle the states of the entity on the server as it is a server script which is running and processing the states. Do you have more information about how you set yours up or handling other things that branch off the entity?

i have something similar, but i enable or disable the scripts on enemy if a player is within a range, no point them running if they are a good distance away. it wont cover everything ur doing but its a bit of hack and might help. then i can have all enemy on server

1 Like

The entity are always going to be near in or in view of the player. This wont work at all really or could degrade enjoyment and cause other issues potentially. Have you played games like WWZ?

Aim for Simulated AI

What I mean by this is to simulate character movement by simply downing the calculations abit, this means that the enemy AI should some what be client based as the simpler physics should be calculated on the client.

I suggest you should look into:

3 Likes

I will take a look at the links you have linked, I will also look into simulated ai a bit more too.

I am planning on handling the AI on the client (not functionality but models and things like that) to help reduce server load. Thanks. Any other info or more in-depth stuff would be handy if you know anything relevant or useful in general.

I recommend using behavior trees even though it does not solve the many enemies problem.

1 Like

I am not bothered about handling behaviour of the AI I am talking about purely handling the amount of AI and also the display and updating of the AI too in not only an efficient but performant manor.

For future reference if it isn’t about the topic please do not reply with it as it is just redundant.


I have also used these before.

My aproach might not be most optimized, because i aimed for about 400 pets, but they also have additional behaviors

What i’ve did is i combined ECS game loop with OOP pets (if you aim for performance, use Data oriented design as it’s more aimed at performance) then i use replicate method to replicate buffer to a client handler, and then call specific action based off buffer, i don’t send anything apart of numbers, as it’s more convienient than sending instances or strings and way more optimized

You do rendering on client, if you require physics, don’t use Roblox Physics, make simple system based off your game, raycast or grid should do

Also i used batching per player, i would have max 10 players in my game, so i can update each player every 0.7 seconds, with 0.07 cooldown per player

EDIT: Other thing you could use is scale effect, it means you remove amount of enemies and buff their stats to make them feel a lot, pretty much more than 200 enemies isn’t visible in games especially with smaller maps

2 Likes

I guess you’re using buffer reading and writing on the client and sending buffer data to the cli from the server then handling specific things as its faster I assume.

This whole systems sounds interesting and I may “try” to replicate most of these systems.

Editing the stats of the entity is NOT viable as I am going to have gameplay functions that really do rely on the amount of entities there are in a given space like how they will take down a fence due to their numbers and other things.


If you can or want to would you be more inclined to go or dive a bit deeper in the inner workings of the buffer system or the system as a whole?

Buffer is only a small optimization, the real one is precise batching and ECS loop, in short i update the game every frame, and each 0.07 second i update next player and his pets, with maximum of 50 pets, i would do 50 replicate calls, tho i should really be sending them in one call as it’s very inefficient to send many remotes due to overhead

Batching works like this:

  • There’s update queqe with 10 spots
  • Each time player joins / leaves his PlayerObject is added to a list
  • Each 0.07 second i update next player
  • Pets move in incremental way, so i don’t worry about lerping or time

This is my updater:

function PetsUpdater.updatePets(DeltaTime: number)
	Accumulator += DeltaTime
	
	local Delta = Accumulator - Cooldown
	if Delta > Cooldown then
		Accumulator -= Delta
		
		local Pets= UpdateList[CurrentIndex]
		CurrentIndex += 1
		
		if CurrentIndex > #UpdateList then
			CurrentIndex = 1
		end

		if not Pets then
			return
		end
		
		for _, Pet in Pets do
			Pet:move(Delta * 10)
			
			local MoveDelay = math.random(1, 10) / 100
			task.wait(MoveDelay)
		end
	end
end

EDIT: With about 400 pets i have smaller lag, but i don’t think i would use that many entities at once

1 Like

Interesting idea/way to update the entities. Not too sure how viable this would be with a swarm system. Trying to mainly replicate the “Swarm Engine” from WWZ but this is a cool thing to look at nun the less.

Btw, you gave me idea to implement batching, with estimated calculations, i can say i could handle up to 5000 pets at once with batching, as for now thousands of enemies require somewhat high bandwidth

Happy your inspired but still not exactly sure about my implementing something of the sort into my own.

1 Like

I highly suggest reading about ECS - Entity Composition System as it’s paradigm designed to deal with entities and large amount of objects

1 Like

I will look into this. Any resources you could suggest? Or is it that type definition system/modular?

So I found this ECS-Lua - A tiny and easy to use ECS (Entity Component System) engine but it seems quite old and some things might be outdated. Does it hold up well still in terms of its stability and performance?

The docs havent been updated neither and there isn’t really any definitions I believe.

Can you say confidently that this is worth investing time to though its nearly half a decade old? @0786ideal

I wrote my own ECS inspired game loop, ECS for entities might be simply physics loop + data updates, you should read about it in other languages like c++ or python or java script, where ECS is used more often

1 Like

Oh ok I see. I mean I would be more interested in an already existing and maintained system of ECS. I have 0 interest in building a system like that for a game that likely is never going to go far.

1 Like