What is a better way to get handle AI?

I’m sure we have all played something that consists of NPC’s that move from a point to another even on mobile like Clash Royale or Clash Of Clans and on PC League Of Legends, Dota, World Of Warcraft, and MMOs.

So, I am more then capable of creating a NPC’s system where it locates a target within a radius and goes after the target but the problem is I want to make this as resource friendly as possible.

Let’s suppose I need to manage 200 npc’s at one time. The Npc’s will be given a path to move to but if it detects any enemy within a range, it will go off it’s original path and follow the enemy via Pathfinding. So my question is how would I do this? My current idea is this:

local lastUpdated = {} 
local updateTime = 0.2

while true do
wait()
for i, v in pairs(workspace.Minions:GetChildren()) do
if tick() - lastUpdated[v] <= updateTime then -- assuming when I spawn in the minions I log it into the table
-- here we get target, create a path and follow etc
lastUpdated[v] = tick()
end
end
end

My current way prevents updating all the minions at once and gives the minions update times but I was wondering what would be the best way to approach this?

Edit; I have received good information but if you would like to let’s me know of more ways please reply

The biggest problem with this method is that your using getchildren each step of the loop which can be pretty resource intensive, Typically for my AI I have a table that has every AI and a function when creating an AI gets add into that table aswell as any AI’s in workspace also get added during the start of the game. I then have a main loop that loops through the entire table under a single thread.

2 Likes

Clarification:
Basically it’s better to store the AI into a table and iterate through that table to get the AI compared to :GetChildren().

That is correct you be cutting down a lot of steps if you were to do that

Also for pathfinding and stuff, I have it where each AI in my table has many properties and functions/methods built in

example

AITable[Ai].CurrentPath – returns an array table

Thanks, never thought of that.

I’d assume I do something like so:

local AIS = {
[ExampleAiObj] = {
Properties = {},
Functions = {},
}
} 

AIS[ExampleAiObj].Functions.getPath = function(args) -- I'd assume this would create the function 

end

AIS[ExampleAiObj].Functions.getPath(args) -- And I'd assume this would work

EDIT: You’ve been very helpful I’m still going to wait for more replies to see if I can get some more information on this topic.

1 Like

Nevermore Engine has this Binder Class that I believe could really simplify the creation of such a system. What it does is attaching a class to all instances with the CollectionService tag you specify. I have not used this myself but it might be worth taking a look at :slight_smile:

Nevermore Engine Binder

If you have not used classes before I would recommend reading this:
All about Object Oriented Programming

2 Likes