Create a function condition that require the player to be inside a zone
8 replies, posted
[U][B]Hello facepunch:[/B][/U]
I need a little help on configuring the Bio-Hazard addon from the workshop.
Here' s the thing :
There' s an entity that spawns a toxic sphere that is suppose to infect you once you get in.
The thing is that there' s a condition missing, so you get infected where ever you are on the map.
here' s the code :
[CODE]self.skybox = ents.Create("prop_dynamic")
self.skybox:SetModelScale( self.skybox:GetModelScale() * 10, 0 ) -- Used to define the sphere size
self.skybox:SetMaterial("models/debug/debugwhite")
--self.skybox:SetRenderMode(RENDERMODE_TRANSALPHA)
self.skybox:SetColor(Color(10,255,0,255))
self.skybox:SetPos(self:GetPos())
self.skybox:SetAngles(self:GetAngles())
self.skybox:Spawn()
self.skybox:SetSolid(SOLID_NONE)
self.skybox:SetParent(self)
end
function ENT:InfectionNormal(ent)
if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true then return end -- Missing condition here
if (ent:GetClass()=="npc_metropolice" or ent:GetClass()=="npc_combine_s") and ent:Health() >= ent:GetMaxHealth() then return end
if ent:GetClass()=="npc_zombie" or ent:GetClass()=="npc_zombine" or ent:GetClass()=="npc_fastzombie" or ent:GetClass()=="npc_poisonzombie" or ent:GetClass()=="npc_zombie_torso" or ent:GetClass()=="npc_headcrab_black" or ent:GetClass()=="npc_headcrab" or ent:GetClass()=="npc_fastzombie_torso" or ent:GetClass()=="npc_headcrab_fast" or ent:GetClass()=="npc_headcrab_poison" then return end
ent.Infect = (ent.Infect +2*GetConVarNumber("ba_multiplier_inf")*self.SatusMulti)
local infectedvirus = DamageInfo()
infectedvirus:SetDamage(math.random(0,1)*self.SatusMulti)
infectedvirus:SetDamageType(DMG_ACID)
infectedvirus:SetAttacker(self)
infectedvirus:SetInflictor(self)
ent:TakeDamageInfo(infectedvirus)
end[/CODE]
So i pretty much need a condition to do :
[CODE]if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true or not ent."is-inside" self.skybox:GetModelScale() * 10, 0 then return end[/CODE]
So the player does not get infected if he isnt inside the zone drawed by the sphere.
I hope you understood what i meant to say, i can give more infos and ressources if needed.
Thanks :)
You can either check if the player is inside the list returned by [img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/ents/FindInSphere]ents.FindInSphere[/url], or more efficiently, check if the player's distance ([img]http://wiki.garrysmod.com/favicon.ico[/img] [url=http://wiki.garrysmod.com/page/Vector/DistToSqr]Vector:DistToSqr[/url]) from the centre of the sphere is less than the radius.
[QUOTE=DG_Mathieu;52273091][U][B]Hello facepunch:[/B][/U]
I need a little help on configuring the Bio-Hazard addon from the workshop.
Here' s the thing :
There' s an entity that spawns a toxic sphere that is suppose to infect you once you get in.
The thing is that there' s a condition missing, so you get infected where ever you are on the map.
here' s the code :
[CODE]self.skybox = ents.Create("prop_dynamic")
self.skybox:SetModelScale( self.skybox:GetModelScale() * 10, 0 ) -- Used to define the sphere size
self.skybox:SetMaterial("models/debug/debugwhite")
--self.skybox:SetRenderMode(RENDERMODE_TRANSALPHA)
self.skybox:SetColor(Color(10,255,0,255))
self.skybox:SetPos(self:GetPos())
self.skybox:SetAngles(self:GetAngles())
self.skybox:Spawn()
self.skybox:SetSolid(SOLID_NONE)
self.skybox:SetParent(self)
end
function ENT:InfectionNormal(ent)
if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true then return end -- Missing condition here
if (ent:GetClass()=="npc_metropolice" or ent:GetClass()=="npc_combine_s") and ent:Health() >= ent:GetMaxHealth() then return end
if ent:GetClass()=="npc_zombie" or ent:GetClass()=="npc_zombine" or ent:GetClass()=="npc_fastzombie" or ent:GetClass()=="npc_poisonzombie" or ent:GetClass()=="npc_zombie_torso" or ent:GetClass()=="npc_headcrab_black" or ent:GetClass()=="npc_headcrab" or ent:GetClass()=="npc_fastzombie_torso" or ent:GetClass()=="npc_headcrab_fast" or ent:GetClass()=="npc_headcrab_poison" then return end
ent.Infect = (ent.Infect +2*GetConVarNumber("ba_multiplier_inf")*self.SatusMulti)
local infectedvirus = DamageInfo()
infectedvirus:SetDamage(math.random(0,1)*self.SatusMulti)
infectedvirus:SetDamageType(DMG_ACID)
infectedvirus:SetAttacker(self)
infectedvirus:SetInflictor(self)
ent:TakeDamageInfo(infectedvirus)
end[/CODE]
So i pretty much need a condition to do :
[CODE]if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true or not ent."is-inside" self.skybox:GetModelScale() * 10, 0 then return end[/CODE]
So the player does not get infected if he isnt inside the zone drawed by the sphere.
I hope you understood what i meant to say, i can give more infos and ressources if needed.
Thanks :)[/QUOTE]
It would depend on the shape of the zone. If it were cuboid, you would check something like this:
[lua]
if ( min.x < position.x and position.x < max.x ) and ( min.y < position.y and position.y < max.y ) and ( min.z < position.z and position.z < max.z ) then ...
[/lua]
If the shape is a sphere, you would do something like this:
[lua]
if ( position.distance( sphereOrigin.position ) < radiusShere ) then
[/lua]
Please note that the above is pseudo code, the actual functions you would use are available on the wiki. This is only an example of the logic you would use.
Vector:DistToSqr seems to be the best one to do, but im not sure how to "link" it with the sphere it self. Its pretty new to me :s
[editline]25th May 2017[/editline]
[QUOTE=James xX;52273107]
[lua]
if ( position.distance( sphereOrigin.position ) < radiusShere ) then
[/lua]
Please note that the above is pseudo code, the actual functions you would use are available on the wiki. This is only an example of the logic you would use.[/QUOTE]
Yea thats what i planned to do with Vector:DistToSqr and then just compare it to the radius, but i' ve never used it before its kinda new to me x)
[editline]25th May 2017[/editline]
Do you use it like :
[CODE]if Vector:DistToSqr(skybox, ent) >= 3000 then return end[/CODE]
No, you run the function on a vector and put in another vector as the argument. So it would look like
[code]if (SomeEntity:GetPos():DistToSqr (SpherePos) <= SphereRadius) then [/code]
[QUOTE=code_gs;52273178]
[code]if (SomeEntity:GetPos():DistToSqr (SpherePos) <= SphereRadius) then [/code][/QUOTE]
is "SpherePos" the location defined by :
[CODE]self.skybox:SetPos(self:GetPos())[/CODE]
Cause the sphere im trying to compare it to is a prop_dynamic entity so im not sure to understand, or if its the right thing to do :s
I got something like that :
[CODE]self.skybox = ents.Create("prop_dynamic")
self.skybox:SetModelScale( self.skybox:GetModelScale() * 10, 0 )
self.skybox:SetMaterial("models/debug/debugwhite")
--self.skybox:SetRenderMode(RENDERMODE_TRANSALPHA)
self.skybox:SetColor(Color(10,255,0,255))
self.skybox:SetPos(self:GetPos())
self.skybox:SetAngles(self:GetAngles())
self.skybox:Spawn()
self.skybox:SetSolid(SOLID_NONE)
self.skybox:SetParent(self)
end
function ENT:InfectionNormal(ent)
if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true or (ent:GetPos():DistToSqr (skybox:GetPos()) <= 3000) then return end[/CODE]
but it gives me the error :
ERROR : attempt to index global 'skybox' (a nil value)
That's because you defined it as self.Skybox. just change "skybox" to "self.skybox" on that last line.
[CODE](ent:GetPos():DistToSqr (self.skybox:GetPos()) >= 3000)[/CODE]
Changed it like that and printed me that : "Tried to use a NULL entity!"
Thing is that self.skybox is defined in an other function, so i dont think it will work in that new one.
[editline]25th May 2017[/editline]
I also tried :
[CODE]function ENT:LoadEffects()
self.skybox = ents.Create("prop_dynamic")
self.skybox:SetModelScale( self.skybox:GetModelScale() * 10, 0 )
self.skybox:SetMaterial("models/debug/debugwhite")
--self.skybox:SetRenderMode(RENDERMODE_TRANSALPHA)
self.skybox:SetColor(Color(10,255,0,255))
self.skybox:SetPos(self:GetPos())
self.skybox:SetAngles(self:GetAngles())
self.skybox:Spawn()
self.skybox:SetSolid(SOLID_NONE)
self.skybox:SetParent(self)
end
function ENT:InfectionNormal(ent)
if (ent.weargasmask == true and ent.weargasmaskfilter == true) or ent.ZombieBA == true or ent.heroguy == true or (ent:GetPos():Distance( self.skybox:GetPos() ) >= 3000) then return end[/CODE]
Still didnt work : attempt to index global a nil value
Check if self.skybox is valid before using it.
Sorry, you need to Log In to post a reply to this thread.