Which out of these two is a better way for optimization sake:[LUA]hook.Add("Think", "ENT1_think", function()
for _, ent in pairs(ents.FindByClass("ent1")) do
--Code ent 1
end
end)
hook.Add("Think", "ENT2_think", function()
for _, ent in pairs(ents.FindByClass("ent2")) do
--Code ent 2
end
end)
hook.Add("Think", "ENT3_think", function()
for _, ent in pairs(ents.FindByClass("ent3")) do
--Code ent 3
end
end)
hook.Add("Think", "ENT4_think", function()
for _, ent in pairs(ents.FindByClass("ent4")) do
--Code ent 4
end
end)[/LUA]or this[LUA]hook.Add("Think", "ENT_All_think", function()
for _, ent in pairs(ents.GetAll()) do
if ent:GetClass() == "ent1" then
--Code ent 1
elseif ent:GetClass() == "ent2" then
--Code ent 2
elseif ent:GetClass() == "ent3" then
--Code ent 3
elseif ent:GetClass() == "ent4" then
--Code ent 4
end
end
end)[/LUA]To me it looks like the second one is less expensive, but I could be wrong.
[LUA]
local EntClasses = {ent1=true, ent2=true, ent3=true, ent4=true}
hook.Add("Think", "ENT_All_think", function()
for _, ent in pairs(ents.GetAll()) do
local class = ent:GetClass()
if(!EntClasses[class]) then continue end
if class == "ent1" then
--Code ent 1
elseif class == "ent2" then
--Code ent 2
elseif class == "ent3" then
--Code ent 3
elseif class == "ent4" then
--Code ent 4
end
end
end)
[/LUA]
I think the FindByClass function runs through all of the entities and checks its class anyways, but if you want to go with FindByClass you should just stick with one hook.
Sorry if it took you long to optimize my code even more, but I won't use that, since some of the entitys class is unknown. Thanks anyway. For the answer.
Sorry, you need to Log In to post a reply to this thread.