• Health Kit Entity | Help!
    7 replies, posted
Hey Facepunch user(s) I've having an issue with a health kit I made. When you're using the Health kit it suppose to delete the entity, but it doesn't. And if you're having 99 HP and if you use it you'll get 124 HP.. -- Sorry, if this is more a request then a question.. Yes I did delete the Remove function so I could handle this over to you guys.. Thanks in advanced! :-) [lua] AddCSLuaFile( "shared.lua" ) include("shared.lua") function ENT:Initialize() self:SetModel("models/Items/HealthKit.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end self:SetUseType( SIMPLE_USE ) IsAttacked = false IsUsed = false end -- Called when a activator uses the item. function ENT:Use( activator, self ) if ValidEntity(self) then if ValidEntity(activator) then local health = activator:Health() if activator:IsPlayer() && IsUsed == true && activator:Health() >= 100 then Notify(activator, 1, 4, string.format(LANGUAGE.unable, "You do not need any more health!", "")) end if activator:IsPlayer() && health < 100 && IsUsed == false then activator:SetHealth( 25 + health) activator:EmitSound( "items/medshot4.wav", 50, 100) IsUsed = true timer.Create("HealthKitTimer", 3, function() timer.Destroy("HealthKitTimer") SafeRemoveEntity(self) end) end if health >= 100 then activator:SetHealth(100) end end end end [/lua]
A few things: 1. You have your Use function set up with a "self" parameter. This is stomping all over the built-in self parameter, so your SafeRemoveEntity(self) is actually trying to remove whatever is passed in as the caller for the use function. [b][url=wiki.garrysmod.com/?title=ENT.Use]ENT.Use [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] So, change [lua]function ENT:Use( activator, self )[/lua] to [lua]function ENT:Use( activator, caller)[/lua] If all you're doing with the timer is removing the entity after a delay, you might want to look into [b][url=http://wiki.garrysmod.com/?title=G.SafeRemoveEntityDelayed]G.SafeRemoveEntityDelayed [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] instead of setting up your own timer. 2. Your code at the end to check for health >= 100 doesn't work because health is a local variable that you set before you change the player's health. This means that the value of health will be the player's health [i]before[/i] you add any health, so your code never actually handles the >100 case. You can do [lua]if activator:IsPlayer() && activator:Health() >= 100 then[/lua] or, you could change your actual SetHealth call to use something like [b][url=http://wiki.garrysmod.com/?title=Math.Clamp]Math.Clamp [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] and do away with the later if check completely: [lua]activator:SetHealth(math.Clamp(25 + health), 0, 100)[/lua] 3. It looks like you're using IsAttacked and IsUsed as global variables. Is this intended, or do you really want to do self.IsAttacked and self.IsUsed? Global means every health kit uses the same values, while using self would mean each health kit has their own values. Leaving them global may cause strange behavior if more than one health kit is spawned.
I already knew that and tried that. It didn't work, at all. But I didn't know about using global as you sufficed.
Oh, there was one more thing that I forgot. [b][url=http://wiki.garrysmod.com/?title=Timer.Create]Timer.Create [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] takes an additional argument. If you still want to use it, you need to modify it to be: [lua]timer.Create("HealthKitTimer", 3, 1, function() ... [/lua] What else did you try that didn't work?
Well,[b][url=http://wiki.garrysmod.com/?title=G.SafeRemoveEntityDelayed]G.SafeRemoveEntityDelayed [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] didn't want to run, all my global removes does nothing. And when i use [b][url=http://wiki.garrysmod.com/?title=Entity.Remove]Entity.Remove [img]http://wiki.garrysmod.com/favicon.ico[/img][/url][/b] then it also removes the player. So it's a bit confusing, i have tried to make a other function do it but that didn't work so well. Do you have any ideas?
You changed the function to ENT:Use(activator, caller) and self:Remove is still removing the player?
Yes, I did.
Below is my test code. It works just fine for me. I changed the timer to SafeRemoveEntityDelayed because all instances of the health kit used the same timer name, so destroying the timer screwed up the removal of entities if there were multiple health kits recently used. (I commented out Notify since I guess that's a DarkRP-specific command and I don't have it) [lua] AddCSLuaFile( "shared.lua" ) include("shared.lua") function ENT:Initialize() self:SetModel("models/Items/HealthKit.mdl") self:PhysicsInit(SOLID_VPHYSICS) self:SetMoveType(MOVETYPE_VPHYSICS) self:SetSolid(SOLID_VPHYSICS) local phys = self:GetPhysicsObject() if phys:IsValid() then phys:Wake() end self:SetUseType( SIMPLE_USE ) self.IsAttacked = false self.IsUsed = false end -- Called when a activator uses the item. function ENT:Use( activator, caller ) if !ValidEntity(self) or !ValidEntity(activator) then return end local health = activator:Health() if activator:IsPlayer() && self.IsUsed == true && activator:Health() >= 100 then --Notify(activator, 1, 4, string.format(LANGUAGE.unable, "You do not need any more health!", "")) print("You do not need any more health!") end if activator:IsPlayer() && health < 100 && self.IsUsed == false then activator:SetHealth(math.Clamp( 25 + health, 0, 100)) activator:EmitSound( "items/medshot4.wav", 50, 100) self.IsUsed = true SafeRemoveEntityDelayed(self, 3) end end [/lua]
Sorry, you need to Log In to post a reply to this thread.