• Changing weapon firerate (and maybe reload) on entity use?
    17 replies, posted
Trying to make a SEnt that doubles the rate of fire on the users sweps. I know obviously I can't set it up so it works on every single weapon in gmod, so I'm just trying to set it up so it works with some CS:S guns I remade using Garry's old CS SWep base. in each CS weapon, I have this [lua]delay = (some number less than 1) SWEP.Primary.Delay = delay [/lua] The way I have my Ent set up is it (supposedly) replaces "delay" with "delay / 2." Thing is, I have no idea how to actually make it do that. Here's what I have in the Ent's code at the moment. [lua]function ENT:Use( activator, caller ) for k, v in pairs(activator:GetWeapons()) do delay = (delay / 2) end end [/lua] I also tried this (without success) [lua]function ENT:Use( activator, caller ) delay = (delay / 2) end [/lua] All feedback appreciated.
SWEP.Primary.Delay in most cases is shared, so you'll have to set it on both instances of the client and server, not just the server.
[QUOTE=Nornan12;42483392]Trying to make a SEnt that doubles the rate of fire on the users sweps. I know obviously I can't set it up so it works on every single weapon in gmod, so I'm just trying to set it up so it works with some CS:S guns I remade using Garry's old CS SWep base. in each CS weapon, I have this [lua]delay = (some number less than 1) SWEP.Primary.Delay = delay [/lua] The way I have my Ent set up is it (supposedly) replaces "delay" with "delay / 2." Thing is, I have no idea how to actually make it do that. Here's what I have in the Ent's code at the moment. [lua]function ENT:Use( activator, caller ) for k, v in pairs(activator:GetWeapons()) do delay = (delay / 2) end end [/lua] I also tried this (without success) [lua]function ENT:Use( activator, caller ) delay = (delay / 2) end [/lua] All feedback appreciated.[/QUOTE] You're currently setting just a random variable, reload, to that number. reload is used at the beginning, during the init, but never looked at again.
[QUOTE=brandonj4;42483443]SWEP.Primary.Delay in most cases is shared, so you'll have to set it on both instances of the client and server, not just the server.[/QUOTE] (I don't think I explained the extent of my n00biness very well) How do I do that? I tried copying the function to cl_init.lua and adding "if SERVER or if CLIENT" in the function in init.lua but no difference (I apologize for my ignorance)
[QUOTE=Nornan12;42483520](I don't think I explained the extent of my n00biness very well) How do I do that? I tried copying the function to cl_init.lua and adding "if SERVER or if CLIENT" in the function in init.lua but no difference (I apologize for my ignorance)[/QUOTE] The cl_ in front of cl_init means it's clientside. init is serverside. There should be a shared.lua file, put it in that.
[QUOTE=Nornan12;42483520](I don't think I explained the extent of my n00biness very well) How do I do that? I tried copying the function to cl_init.lua and adding "if SERVER or if CLIENT" in the function in init.lua but no difference (I apologize for my ignorance)[/QUOTE] When you press E on your entity, you're going to have to network the SWEP's delay variable to change clientside aswell since "ENT:Use()" is a serverside hook. You can do this with usermessage or by using the net library.
[QUOTE=Nornan12;42483392]Trying to make a SEnt that doubles the rate of fire on the users sweps. I know obviously I can't set it up so it works on every single weapon in gmod,[/QUOTE] No? I think you can. I mean, if I'm correct you don't HAVE to modify the delay by creating an entirely new global variable, you can probably just put self.Primary.Delay = (self.Primary.Delay * 0.5).
Here's some code that should put you in the right direction. [lua] if SERVER then util.AddNetworkString("SWEP_Change_Firerate") --Cache Message net.Start("SWEP_Change_Firerate") --You're going to have to decide what SWEP is net.WriteInt(SWEP:EntIndex(), 16) --WriteInt for the Entities index instead of WriteEntity for PVS issues net.Broadcast() else net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay/2) --Set it clientside for all players end) end [/lua]
Can you tell me how to define SWEP? I initially put in "SWEP = file.Find("lua/weapons/*.lua", true)" (Which I didn't assume would work, but I honestly can't think of anything else)
[QUOTE=Nornan12;42495298]Can you tell me how to define SWEP? I initially put in "SWEP = file.Find("lua/weapons/*.lua", true)" (Which I didn't assume would work, but I honestly can't think of anything else)[/QUOTE] When you press E on your SENT, "local wep = activator:GetActiveWeapon()". You don't have to call it SWEP, you can change all that if it's less confusing.
It's not that I was confused, I just wasn't sure what to type in to make it understand that SWEP means the weapon (and now that you've explained it, I feel ashamed.) This is what I have now, using your code [lua]function ENT:Use( activator, caller ) if SERVER then util.AddNetworkString("SWEP_Change_Firerate") net.Start("SWEP_Change_Firerate") SWEP = activator:GetWeapons() net.WriteInt(SWEP:EntIndex(), 16) net.Send() else net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay*0.65) end) end end [/lua] adding "activator:GetWeapons()" seemed to work, but now I get "attempt to call method 'EntIndex' (a nil value)" upon using the entity
[QUOTE=Nornan12;42495709]It's not that I was confused, I just wasn't sure what to type in to make it understand that SWEP means the weapon (and now that you've explained it, I feel ashamed.) This is what I have now, using your code [lua]function ENT:Use( activator, caller ) if SERVER then util.AddNetworkString("SWEP_Change_Firerate") net.Start("SWEP_Change_Firerate") SWEP = activator:GetWeapons() net.WriteInt(SWEP:EntIndex(), 16) net.Send() else net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay*0.65) end) end end [/lua] adding "activator:GetWeapons()" seemed to work, but now I get "attempt to call method 'EntIndex' (a nil value)" upon using the entity[/QUOTE] You're not using the syntax in the file how you should. Just hold on i'll post what you should do. [B]I'm assuming you're using a shared file for your SENT.[/B] [lua] util.AddNetworkString("SWEP_Change_Firerate") --This should be at the top of your file after AddCSLuaFile() function ENT:Use(activator, caller) -- THIS IS SERVERSIDE ONLY if !IsValid(activator) or !activator:IsPlayer() then return end local wep = activator:GetActiveWeapon() if !IsValid(wep) then return end net.Start("SWEP_Change_Firerate") net.WriteInt(wep:EntIndex(), 16) net.Send(activator) end if CLIENT then net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay*0.65) end) end [/lua]
"[ERROR]...: attempt to call field 'AddNetworkString' (a nil value)" :|
util.AddNetworkString() is serverside function so if you put it in shared code you have to wrap it in an "if SERVER then ... end" block so the client ignores it
[lua] AddCSLuaFile( "shared.lua" ) AddCSLuaFile( "cl_init.lua" ) if SERVER then util.AddNetworkString("SWEP_Change_Firerate") end ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Light Bolt" ENT.Author = "McManager" ENT.Spawnable = true ENT.AdminSpawnable = true function ENT:Use(activator, caller) -- THIS IS SERVERSIDE ONLY if !IsValid(activator) or !activator:IsPlayer() then return end local wep = activator:GetActiveWeapon() if !IsValid(wep) then return end net.Start("SWEP_Change_Firerate") net.WriteInt(wep:EntIndex(), 16) net.Send(activator) end if CLIENT then net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay*0.65) end) end [/lua] No errors or other feedback from the game, but the firerate remains the same. I'm not totally sure, but I think that if I read more on Gmod lua online and toy around with what you guys have provided me with, I can probably make it work in time. Thanks a lot for your remarkable patience, guys.
I don't know why you're putting "if CLIENT then" inside of an init.lua file when it's only serverside. Take that block of code out and remove the "if CLIENT then" along with it's "end", and use that inside your cl_init.lua file. [editline]11th October 2013[/editline] This doesn't look like a properly made SENT either, you don't have an initialize or anything to go with it.
I have initialize and spawnfunction in init.lua, everything else is in shared (I made a few other sents that were significantly simpler in which I had most of the code in init.lua, and being the simpleton I've proven myself to be, I figured setting everything up in init.lua would work effortlessly again.) [editline]12th October 2013[/editline] Just to verify I have everything in order, here are all three lua files. init.lua [lua]AddCSLuaFile( "cl_init.lua" ) AddCSLuaFile( "shared.lua" ) include('shared.lua') function ENT:Initialize() self:SetUseType(SIMPLE_USE) self:SetModel( "models/codwaw/beat the zombie/perk a cola machines/doubletapon.mdl" ) self:PhysicsInit( SOLID_VPHYSICS ) self:SetMoveType(MOVETYPE_NONE) self:SetSolid( SOLID_VPHYSICS ) local phys = self:GetPhysicsObject() if (phys:IsValid()) then phys:Wake() end end function ENT:SpawnFunction( ply, tr ) if ( !tr.Hit ) then return end local SpawnPos = tr.HitPos local ent = ents.Create( "perk_doubletap" ) ent:SetPos( SpawnPos ) ent:Spawn() ent:Activate() return ent end function ENT:Think() end [/lua] cl_init.lua [lua]include('shared.lua') function ENT:Initialize() end function ENT:Draw() self.Entity:DrawModel() end net.Receive("SWEP_Change_Firerate", function(len) local swepID = net.ReadInt(16) local SWEP = Entity(swepID) if !IsValid(SWEP) or !SWEP.Primary or !SWEP.Primary.Delay then return end SWEP.Primary.Delay = (SWEP.Primary.Delay*0.65) end) [/lua] shared.lua [lua] AddCSLuaFile( "shared.lua" ) AddCSLuaFile( "cl_init.lua" ) if SERVER then util.AddNetworkString("SWEP_Change_Firerate") end ENT.Type = "anim" ENT.Base = "base_gmodentity" ENT.PrintName = "Doubletap Machine" ENT.Author = "McManager" ENT.Spawnable = true ENT.AdminSpawnable = true function ENT:Use(activator, caller) if !IsValid(activator) or !activator:IsPlayer() then return end local wep = activator:GetActiveWeapon() if !IsValid(wep) then return end net.Start("SWEP_Change_Firerate") net.WriteInt(wep:EntIndex(), 16) net.Send(activator) end [/lua]
You'll get more compatibility if you do it this way: [code] local meta = debug.getregistry( ).Weapon local delta = 0 meta.oPrimary = meta.oPrimary or meta.SetNextPrimaryFire meta.oSecondary = meta.oSecondary or meta.SetNextSecondaryFire function meta:SetNextPrimaryFire( when ) if self.Owner:GetNWBool( "DoubleTime" ) then delta = when - CurTime( ) self:oPrimary( CurTime( ) + delta * .5 ) else self:oPrimary( when ) end end function meta:SetNextSecondaryFire( when ) if self.Owner:GetNWBool( "DoubleTime" ) then delta = when - CurTime( ) self:oSecondary( CurTime( ) + delta * .5 ) else self:oSecondary( when ) end end [/code] This way it affects a great majority of the weapons, excluding ones that track their own times and the engine weapons. You'll probably want to use something other than an NWBool, but this is just an example.
Sorry, you need to Log In to post a reply to this thread.