|
Login to ZARP
|
|
Right so I'm working on a crude version of the health/armour chargers but I'm having a bit of bother with implementing an interval timer so the client isn't spammed with a notification every frame while using the charger.
I have a base entity that creates the network variables in the standard ENT:SetupDataTables function, here is the majority of my code from the shared.lua file for the base entity: Warning: Spoiler! [ Click to expand ][ Click to hide ] -- ENTITY EXTENSION FUNCTIONS
function ENT:SetupDataTables()
self:NetworkVar("Int", 0, "Charge")
self:NetworkVar("Int", 1, "NotificationInterval")
end
function ENT:NotifyClient(ply, text, duration, notificationType)
if not self:CanSendNotification() then return end
if (SERVER && duration >= 0) then
net.Start("notifyclient")
net.WriteString(notificationType)
net.WriteString(text)
net.WriteInt(duration, 16)
net.Send(ply)
elseif (CLIENT) then
SendClientNotification(text, notificationType, duration)
end
end
function ENT:CanSendNotification()
return self.timer + self:GetNotificationInterval() > CurTime()
end
-- OTHER FUNCTIONS
function SendClientNotification(text, notificationType, duration)
-- can easily add more error types if required
if (notificationType == "Error") then
notification.AddLegacy(text, NOTIFY_ERROR, duration)
elseif (notificationType == "Generic") then
notification.AddLegacy(text, NOTIFY_GENERIC, duration)
end
surface.PlaySound("buttons/button15.wav")
end
if (CLIENT) then
net.Receive("notifyclient", function()
local notificationType = net.ReadString()
SendClientNotification(net.ReadString(), notificationType, net.ReadInt(16))
end)
endHere is my init.lua for my base entity, where I set the NotificationInterval value on initialise: Warning: Spoiler! [ Click to expand ][ Click to hide ] AddCSLuaFile("cl_init.lua")
AddCSLuaFile("shared.lua")
include("shared.lua")
function ENT:Initialize()
self:SetModel("models/props_c17/consolebox01a.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.timer = CurTime()
self:SetNotificationInterval(4)
end
function ENT:Think()
if CurTime() > self.timer + self:GetNotificationInterval() then self.timer = CurTime() end
end
if ( SERVER ) then util.AddNetworkString("notifyclient") endI have then created a health charger entity that inherits from the base entity briefly described above. In my ENT:Use function below, you can see me calling the base ENT:NotifyClient function I've written when the player's health is full: function ENT:Use(activator, caller)
if not IsValid(caller) or not caller:IsPlayer() then return end
local plyCurrentHealth = caller:Health()
if plyCurrentHealth >= 100 then BaseClass:NotifyClient(caller, "You are already at full health!", 4, "Error") end
endSo if you refer back to my shared.lua file for my base entity (first spoiler), and look within the NotifyClient function, I call another function by the name of CanSendNotification where I check to see if the interval has been passed. However, I get the following error when I press my use key on the entity in-game: [ERROR] lua/entities/base_charger/init.lua:30: attempt to call method 'GetNotificationInterval' (a nil value)
1. CanSendNotification - lua/entities/base_charger/init.lua:30
2. NotifyClient - lua/entities/base_charger/shared.lua:18
3. unknown - lua/entities/health_charger/init.lua:23I feel like this is gonna be a really silly mistake considering it's taken me this long and I still haven't found what I've done wrong. Any help would be appreciated, and yes, I know my scripts are shite pls no hate. |
|
|
Login or register to post a reply.
|
|
bump
some one help this legend ty |
|
|
Login or register to post a reply.
|
if plyCurrentHealth >= 100 then BaseClass:NotifyClient(caller, "You are already at full health!", 4, "Error") endI think you mean "self" not "BaseClass" |
|
|
...
Login or register to post a reply.
The following user said Thank You: .uzi
|
|
DEADMONSTOR wrote:
if plyCurrentHealth >= 100 then BaseClass:NotifyClient(caller, "You are already at full health!", 4, "Error") endI think you mean "self" not "BaseClass" Yeah you're right, boy am I fuckin stupid just updated the code and works properly now, ty dead |
|
|
Login or register to post a reply.
|
|
Sorry I couldn't get to this sooner men!
|
|
|
Login or register to post a reply.
|

