• How to check if Ent not in Table?
    5 replies, posted
in my case, self.targets = {} for k, v in pairs(ents.FindInSphere(self:GetPos(), 100)) do -- here, how to check if not v in self.targets table? table.insert(self.targets,v) i tried to this for o, p in pairs(self.targets) do if v != p then and this if not self.targets[v] then help me :(
First of all, please use [CODE] tags for your lua, As for your question: use [URL="http://wiki.garrysmod.com/page/table/HasValue"]http://wiki.garrysmod.com/page/table/HasValue[/URL] Like so: [CODE]if not table.HasValue (self.targets,ent) then [/CODE] Ent being the thing you want to check for. Also there are ways to make ur code better but I'm on phone and lazy so some9ne else can reply.and tell u
[QUOTE=Kevlon;52351502]First of all, please use [CODE] tags for your lua, As for your question: use [URL="http://wiki.garrysmod.com/page/table/HasValue"]http://wiki.garrysmod.com/page/table/HasValue[/URL] Like so: [CODE]if not table.HasValue (self.targets,ent) then [/CODE] Ent being the thing you want to check for. Also there are ways to make ur code better but I'm on phone and lazy so some9ne else can reply.and tell u[/QUOTE] thanks ! but is it fine? This function is very inefficient for large tables (O(n)) and should probably not be called in things that run each frame. Instead, consider a table structure such as example 2 below. this warning make me afraid
Both of these things from your original post should actually work: [QUOTE=hyein6790;52351389] i tried to this [CODE] for o, p in pairs(self.targets) do if v != p then [/CODE] and this [CODE] if not self.targets[v] then [/CODE] [/QUOTE] Could you post what your code looks like when you add those in?
[QUOTE=MPan1;52351768]Both of these things from your original post should actually work: Could you post what your code looks like when you add those in?[/QUOTE] ENT:Init() self.targets = {} ENT:Think() ----------------- first -------------------- for k, v in pairs(self:GetPos(), 300) do for o, p in pairs(self.targets()) do if v != p and v:IsValid() then // just not work v:TakeDamage(100,self,self) table.insert(self.targets,v) end end end --------------------------------------- --------------- two ------------------ for k, v in pairs(ents.FindInSphere(self:GetPos(), 300) do if v:IsValid() and not self.targets[v] then //self.targets[v] return nil v:TakeDamage(30,self,self) table.insert(self.targets,v) end end ---------------------------------------
You can't loop through self.targets if it's empty already. Think of how a for loop works. It looks for keys and values in a table. It cannot find any if there's no keys or values, so it can't loop through them. Try this: [CODE] function ENT:Init() self.targets = {} end function ENT:Think() for k, v in pairs(self:GetPos(), 300) do if IsValid(v) and not self.targets[v] then v:TakeDamage(100,self,self) self.targets[v] = true -- remember, with self.targets[v], v is the KEY, not the value. end end end [/CODE] By doing self.targets[v], you are checking if the KEY v exists, not the VALUE v. Therefore, you must set the key to be v if you want it to find it, by doing this: [CODE] self.targets[v] = true -- it doesn't have to be true, as long as it is considered true [/CODE]
Sorry, you need to Log In to post a reply to this thread.