I want to spawn about 6 entities at one time and when the player removes them all I want the server to be able to know that and then the player can move on to the next objective.
Put this in a constant timer. Its just an exampe.
[CODE]if Objective == 1 and #ents.FindByClass("YourEntityClass") <= 0 then
Objective = Objective + 1
end[/CODE]
Make sure you spawn those entities before changing the current objective.
A more efficient way to do it would be by sending a net message.
[url]https://wiki.garrysmod.com/page/Net_Library_Usage[/url]
Would write example but on mobile, I'll update this later.
[CODE]
local amountToSpawn = 6
for i = 1, amountToSpawn do
local prop = ents.Create( "prop_physics" )
prop:SetModel( "your/model" )
prop:SetPos( Vector( x, y, z ) )
prop:Spawn()
end
[/CODE]
Untested, but should spawn 6 props then stop.
A more efficient way would be adding each spawned entity to a table, and then check its number of elements. That way it doesn't have to find by class constantly.
[QUOTE=MaxShadow;49843947]A more efficient way would be adding each spawned entity to a table, and then check its number of elements. That way it doesn't have to find by class constantly.[/QUOTE]
You would still have to network the table in-between the client and server. You could network to the server when the box collect along with its ID.
I don't think that networking the table is necessary. Clients don't need to check anything, it's all handled serverside. He said that all 6 entities are spawned at once, so they are probably spawned by the server. When the objective is complete, he'll have to send a message to the clients, but that's not what he asked now.
[QUOTE=MaxShadow;49844556]I don't think that networking the table is necessary. Clients don't need to check anything, it's all handled serverside. He said that all 6 entities are spawned at once, so they are probably spawned by the server. When the objective is complete, he'll have to send a message to the clients, but that's not what he asked now.[/QUOTE]
Oh yeah, should of read better. Thanks.
but then how can I check if the entity is spawned or not.. The player removes them and then I want to check when they are all gone
[CODE]
-- Your code here for spawning entities (and stored in a table) --
-- Secured entities checks.
local MyEntities={}
local MyEntitiesDestroyed={}
hook.Add("OnEntityCreated","DoMyEntitiesExistOrNot",function(ent)
if !IsValid(ent) then return end
if (ent:GetClass() == "my_entity") then
if (ent:IsValid() and ent:IsInWorld()) then
if (table.HasValue(YourSpawnedEntitiesTable,ent)) then
MyEntities[#MyEntities+1]=ent
else
error("Somethings went wrongs. Unknown entity created. ("..tostring(ent)..")")
end
end
end
end)
hook.Add("EntityRemoved","DoMyEntitiesGotRemoved",function(ent)
if !IsValid(ent) then return end
if (ent:GetClass() == "my_entity") then
if (ent:IsValid() and ent:IsInWorld()) then
if (table.RemoveByValue(MyEntities,ent) == false) then
error("Somethings went wrongs. Couldn't remove "..tostring(ent).." from a table.")
else
MyEntitiesDestroyed[#MyEntitiesDestroyed+1]=ent
if (#MyEntitiesDestroyed == #YourSpawnedEntitiesTable) then
Objective=Objective+1
hook.Remove("OnEntityCreated","DoMyEntitiesExistOrNot")
hook.Remove("EntityRemoved","DoMyEntitiesGotRemoved")
end
end
end
end
end)
[/CODE]
Modify the entities OnRemove functions. This will then get called whenever the entity is removed. For an example you can make a local variable (like "spawned") which is a count of the number of entities spawned. If you then subtract that by 1 every time one of them is removed, you can then see when this is under or equal to 0.
[lua]-- In your spawning code
local oldremove = ent.OnRemove -- save the old function if your entity needs it
ent.OnRemove = function(self)
spawned = spawned - 1
if spawned <= 0 then
-- Proceed with your code
end
oldremove(self) -- call the old remove function if you saved it so whatever it was coded to do before still happens
end[/lua]
You could use anything in place of the spawned variable, even a ents.FindByClass check to just check all of them. You could also use ENTITY:Use instead of the OnRemove function so you know who is the activator (given that using it is what removes it).
Sorry, you need to Log In to post a reply to this thread.