• hook.Add not acting as expected
    4 replies, posted
So I have two functions that I need to run in hooks. "SyncClientGlow" sends a net.Broadcast() message containing a table of entities when the mouse is pressed (debug stuff), and "AddGlow" receives the table and applies a constant halo effect to said entities. For some reason, I have to restart the scripts (sometimes more than once) by saving them again until it decides to work. I'm not sure why. I have this (entirely client side) script that adds the hooks: [CODE] AddCSLuaFile ("hooks.lua") print("Initializing Hooks") hook.Add("GUIMousePressed", "SyncClientGlow", SyncEntities) hook.Add("PreDrawHalos", "AddGlow", ApplyGlow) print("Hooks Added!") [/CODE] And the functions are from my SWEP shared.lua file, which looks like this: (sorry for messy code, I haven't worked in any optimization yet, and I'm fairly new to lua) [CODE]if SERVER then print("SERVER") AddCSLuaFile ("shared.lua") SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = 5 SWEP.Primary.DefaultClip = 5 SWEP.Primary.Automatic = false SWEP.Secondary.Ammo = "AR2AltFire" elseif CLIENT then print("CLIENT") SWEP.Slot = 1 SWEP.SlotPos = 2 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true end function InitEntityTable() if serverGlowEntities == nil then serverGlowEntities = {} end if clientGlowEntities == nil then clientGlowEntities = {} end end function SyncEntities() if SERVER then if serverGlowEntities != nil then util.AddNetworkString("SyncGlow") net.Start("SyncGlow") net.WriteTable(serverGlowEntities) net.Broadcast() else print("serverGlowEntities is nil") end elseif CLIENT then print("Syncing client data") net.Receive("SyncGlow", function() clientGlowEntities = net.ReadTable() end ) end end function ApplyGlow() print("Applying Glow!") net.Receive("SyncGlow", function() clientGlowEntities = net.ReadTable() end ) if clientGlowEntities != nil then print("Applying glow to entities") halo.Add(clientGlowEntities, Color(255, 0, 0), 5, 5, 2, true, true) end end SWEP.PrintName = "Glow Gun" SWEP.Author = " (firegodjr) " SWEP.Instructions = "Left click to fire dart, right click to fire an instant beam." SWEP.Category = "firegodjr's SWeps" SWEP.Spawnable = true SWEP.AdminSpawnable = false function SWEP:FireDart() end function SWEP:FireBeam() if SERVER then -- if the eye trace hit something that isn't the world if self.Owner:GetEyeTrace().HitNonWorld == true then -- if the eye trace return isn't nil if self.Owner:GetEyeTrace() != nil then InitEntityTable() local entity = self.Owner:GetEyeTrace().Entity table.insert(serverGlowEntities, entity) print("Added new glow.") end end end end SWEP.ViewModel = "models/weapons/v_pistol.mdl" SWEP.WorldModel = "models/weapons/w_pistol.mdl" local ShotSound = Sound("Metal.SawbladeStick") function SWEP:PrimaryAttack() self:FireDart() SyncEntities() end function SWEP:SecondaryAttack() self:FireBeam() SyncEntities() end [/CODE] Any ideas? I've looked around for a while but haven't been able to find anyone else with this problem.
You should probably avoid calling [B]net.Receive[/B] inside of a function. Instead just do it once, not inside of a function: [CODE] if SERVER then print("SERVER") AddCSLuaFile ("shared.lua") SWEP.Weight = 5 SWEP.AutoSwitchTo = false SWEP.AutoSwitchFrom = false SWEP.Primary.ClipSize = -1 SWEP.Primary.DefaultClip = -1 SWEP.Primary.Automatic = false SWEP.Primary.Ammo = "none" SWEP.Secondary.ClipSize = 5 SWEP.Primary.DefaultClip = 5 SWEP.Primary.Automatic = false SWEP.Secondary.Ammo = "AR2AltFire" elseif CLIENT then print("CLIENT") SWEP.Slot = 1 SWEP.SlotPos = 2 SWEP.DrawAmmo = false SWEP.DrawCrosshair = true end function InitEntityTable() if serverGlowEntities == nil then serverGlowEntities = {} end if clientGlowEntities == nil then clientGlowEntities = {} end end function SyncEntities() if SERVER then if serverGlowEntities != nil then net.Start("SyncGlow") net.WriteTable(serverGlowEntities) net.Broadcast() else print("serverGlowEntities is nil") end end end if SERVER then util.AddNetworkString("SyncGlow") end if CLIENT then net.Receive("SyncGlow", function() clientGlowEntities = net.ReadTable() end ) end function ApplyGlow() print("Applying Glow!") if clientGlowEntities != nil then print("Applying glow to entities") halo.Add(clientGlowEntities, Color(255, 0, 0), 5, 5, 2, true, true) end end SWEP.PrintName = "Glow Gun" SWEP.Author = " (firegodjr) " SWEP.Instructions = "Left click to fire dart, right click to fire an instant beam." SWEP.Category = "firegodjr's SWeps" SWEP.Spawnable = true SWEP.AdminSpawnable = false function SWEP:FireDart() end function SWEP:FireBeam() if SERVER then -- if the eye trace hit something that isn't the world if self.Owner:GetEyeTrace().HitNonWorld == true then -- if the eye trace return isn't nil if self.Owner:GetEyeTrace() != nil then InitEntityTable() local entity = self.Owner:GetEyeTrace().Entity table.insert(serverGlowEntities, entity) print("Added new glow.") end end end end [/CODE] The same goes for util.AddNetworkString, it should only be done once.
The file name "hooks.lua" is almost certainly not unique. If you have to save the file and it starts working after auto-refresh, then it means another file is overriding yours. Try renaming your lua file.
[QUOTE=Z0mb1n3;50172872]The file name "hooks.lua" is almost certainly not unique. If you have to save the file and it starts working after auto-refresh, then it means another file is overriding yours. Try renaming your lua file.[/QUOTE] Does the auto refresher not take into account path names when it refreshes scripts? What about AddCSLuaFile? I thought it would first look from the path the file is in, otherwise from the base lua directories.
I've changed the filename of hooks.lua to syncing_hooks.lua, but I still have the problem. I suppose I should clarify; the script runs when it should, during the client lua startup. However, while I get the "Initializing Hooks" and "Hooks Added" outputs to the console, the repeated spamming of "Applying Glow!" that I would expect from the AddGlow hook doesn't occur until I've caused the auto refresher to refresh syncing_hooks.lua. Here's my console output before refreshing: [QUOTE]Garry's Mod sandbox gm_flatgrass 1 76561198089870834 Requesting 1 lua files from the server clientside lua startup! [B]Initializing Hooks[/B] [B]Hooks Added![/B] Default clips will be not be modified CLIENT SWEP (weapon_plasmarifle_norm) is derived from non existant SWEP (weapon_sbase) - Expect errors! Failed to load custom font file 'c:/program files (x86)/steam/steamapps/common/garrysmod/garrysmod/workshop/resource/fonts/tnt_bankgothicmb.ttf' Compact freed 1167360 bytes Redownloading all lightmaps [/QUOTE] And after: [QUOTE]Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow! Applying Glow![/QUOTE] So it would seem that the hooks aren't actually added upon startup.
Sorry, you need to Log In to post a reply to this thread.