I’ve been looking into using parallel Lua to optimize NPCs for a project, but there isn’t a whole lot of clear information about the workings of parallel Lua and module scripts.
I use metatables to implement object oriented programming like so:
local entity = {}
entity.__index = entity
function entity:new(o)
local o = o or {}
setmetatable(o, entity)
return o
end
function entity:moveTo(target)
-- Pathfinding logic or something
end
function entity:findNearesttarget()
-- Chase logic
end
return entity
Now, since doing a :GetAllChildren()
every single time I want to look for the nearest target is slow, I have another modulescript somewhere that keeps track of all players/entities that are added that have humanoids.
Players and NPCs are added to a target list when spawned and removed when dead or despawned.
To optimize my NPCs so I can have 100s of them, I want to do the target seeking and pathfinding logic multi-threaded.
However I’m not sure what exactly happens if a module script is require()
'd on another thread.
Will this duplicate all the target adding/removing from a target list for every thread?
And what happens to that list of available humanoids/targets if the thread ends or is synchronized again?
Will every actor have their own personal copy of everything?
All NPCs will likely be actors with a script in them that only has 3 lines of code since all logic is inside modulescripts and uses metatables to keep stuff object-oriented and easily reusable.
I cannot find a lot of detailed information on multi-threading.
I myself have a 16-core / 32-threads CPU and I want to multi-thread my systems over as many cores as there are available to minimize the lag.
My AI logic might get really complex since I plan on giving all AI per-weapon logic (they can pick-up and drop different weapons and spawn with different strategies so they aren’t as dumb and can challenge the player in multiple ways).
I already know how to implement most of it but I want to multi-thread it right away so I don’t end up having to redesign my whole AI system later.
I would greatly appreciate any help or tips, especially knowledge on how this whole parallel Lua thing works.
I’m not unfamiliar with multi-threading but not really used to it either.
I was going to experiment around a little but not entirely sure where to start due a lack of information.
I at least hope I can get the most heavy/intensive AI functions to run parallel since it might get really complex and 100 humanoids with the complex AI I want to implement might lag if all of it were single-threaded.