• Hold-to-aim Ironsights help
    16 replies, posted
Hey guys. I need help coding hold-to-aim ironsights. I've already succeeded in making toggle ironsights but personally they feel kinda clunky. I've tried looking at the M9K base for references and also researching KeyPressed, KeyReleased & KeyDown. But still no luck. Any help would be appreciated. Thanks :D
[code]local function IronSightsToggle( ply, key ) if ( key == IN_ATTACK2 ) then -- Toggle ironsights end end hook.Add( "KeyRelease", "IronSights_Deactivate", IronSightsToggle ) hook.Add( "KeyPress", "IronSights_Activate", IronSightsToggle )[/code]
[QUOTE=code_gs;47390362][code]local function IronSightsToggle( ply, key ) if ( key == IN_ATTACK2 ) then -- Toggle ironsights end end hook.Add( "KeyRelease", "IronSights_Deactivate", IronSightsToggle ) hook.Add( "KeyPress", "IronSights_Activate", IronSightsToggle )[/code][/QUOTE] Thanks. But, where do I put this? Somewhere inside shared or cl_init?
Clientsided
don't use those functions unless you have to. [lua] SWEP.IronSights = false // lets make a bool because we'll need this. function SWEP:SecondaryAttack () if ( not self.IronSight ) then self.IronSights = true end end function SWEP:Think () if ( self.IronSight and not self.Owner:KeyDown ( IN_ATTACK2 )) then self.IronSights = falae end end [/lua] this needs to be the same on client AND server so you need to fuck with it shared.
[QUOTE=robbert^^;47390882]don't use those functions unless you have to. [lua] SWEP.IronSights = false // lets make a bool because we'll need this. function SWEP:SecondaryAttack () if ( not self.IronSight ) then self.IronSights = true end end function SWEP:Think () if ( self.IronSight and not self.Owner:KeyDown ( IN_ATTACK2 )) then self.IronSights = falae end end [/lua] this needs to be the same on client AND server so you need to fuck with it shared.[/QUOTE] Think is a lot more inefficient than the hooks I used. [editline]25th March 2015[/editline] Not to mention that the Think is shared, so it's even worse.
the think has to be shared in order for this to work correctly because the bool has to be the same on server and client assuming he's changing the position of the vieuwmodel. and it will only change the variable once because it will set it to false once run. AND it bad practice in my opinion to jam hooks everywhere when you don't need to because it makes it really painful when you're looking for something.
[QUOTE=code_gs;47390853]Clientsided[/QUOTE] It didn't work. The SWEP disappeared when I put the function you gave me in cl_init.
[QUOTE=Aaronautics;47390937].snip.[/QUOTE] sv_cheats 1; give INSERT_CORRECT_WEAPON_NAME_HERE and if it gives you an error, post it here. edit: hold the phone, aren't those functions shared? :v:my memory tells me they are.
Neither worked [editline]25th March 2015[/editline] [QUOTE=Aaronautics;47391355]Neither worked[/QUOTE] Here's what shared.lua contains btw. NOTE: I'm gonna cut the whole thing into segments because captcha is being a prick. SWEP.PrintName = "SMG" SWEP.Author = "FS_Aaronautics" SWEP.Contact = "" SWEP.Purpose = "" SWEP.Instructions = "" SWEP.Category = "HL2 with Ironsights" SWEP.Spawnable= true SWEP.AdminSpawnable= true SWEP.AdminOnly = false SWEP.ViewModelFOV = 50 SWEP.ViewModel = "models/weapons/c_smg1.mdl" SWEP.WorldModel = "models/weapons/w_smg1.mdl" SWEP.ViewModelFlip = false SWEP.AutoSwitchTo = true SWEP.AutoSwitchFrom = true SWEP.Slot = 3 SWEP.SlotPos = 1 SWEP.UseHands = true SWEP.HoldType = "smg" SWEP.FiresUnderwater = true SWEP.DrawCrosshair = false SWEP.DrawAmmo = true SWEP.ReloadSound = "Weapon_SMG1.Reload" SWEP.Base = "weapon_base" [editline]25th March 2015[/editline] SWEP.Primary.Sound = Sound("Weapon_SuppressedSMG1.Single") SWEP.Primary.Damage = 12 SWEP.Primary.TakeAmmo = 1 SWEP.Primary.ClipSize = 45 SWEP.Primary.Ammo = "SMG1" SWEP.Primary.DefaultClip = 45 SWEP.Primary.Spread = 0.1 SWEP.Primary.NumberofShots = 1 SWEP.Primary.Automatic = true SWEP.Primary.Recoil = 0.20 SWEP.Primary.Delay = 0.07 SWEP.Primary.Force = 4 SWEP.Secondary.ClipSize = 0 SWEP.Secondary.DefaultClip = 0 SWEP.Secondary.Automatic = false SWEP.Secondary.Ammo = "none" [editline]25th March 2015[/editline] SWEP.WElements = { ["stock"] = { type = "Model", model = "models/props_c17/handrail04_cap.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(3, 1.1, -1.3), angle = Angle(2, -90, 79), size = Vector(0.5, 0.5, 0.5), color = Color(255, 255, 255, 255), surpresslightning = false, material = "metal2", skin = 0, bodygroup = {} }, ["silencer"] = { type = "Model", model = "models/props_c17/oildrum001.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(15, 1.5, -7.401), angle = Angle(-100.901, 0, 0), size = Vector(0.079, 0.079, 0.237), color = Color(255, 255, 255, 255), surpresslightning = false, material = "rubber2", skin = 0, bodygroup = {} } } SWEP.VElements = { ["stock"] = { type = "Model", model = "models/props_c17/handrail04_cap.mdl", bone = "ValveBiped.Bip01_R_Hand", rel = "", pos = Vector(-6, -0.401, 1.7), angle = Angle(0, -99.351, 92.337), size = Vector(0.5, 0.899, 0.2), color = Color(255, 255, 255, 255), surpresslightning = false, material = "metal2", skin = 0, bodygroup = {} }, ["silencer"] = { type = "Model", model = "models/props_c17/oildrum001.mdl", bone = "ValveBiped.base", rel = "", pos = Vector(0, -0.5, 10), angle = Angle(0, 0, 0), size = Vector(0.079, 0.079, 0.237), color = Color(255, 255, 255, 255), surpresslightning = false, material = "rubber2", skin = 0, bodygroup = {} } } [editline]25th March 2015[/editline] function SWEP:PrimaryAttack() self.Owner:MuzzleFlash(false) if ( !self:CanPrimaryAttack() ) then return end local bullet = {} bullet.Num = self.Primary.NumberofShots bullet.Src = self.Owner:GetShootPos() bullet.Dir = self.Owner:GetAimVector() bullet.Spread = Vector( self.Primary.Spread * 0.1 , self.Primary.Spread * 0.1, 0) bullet.Tracer = 1 bullet.Force = self.Primary.Force bullet.Damage = self.Primary.Damage bullet.AmmoType = self.Primary.Ammo local rnda = self.Primary.Recoil * -1 local rndb = self.Primary.Recoil * math.random(-1, 1) self:ShootEffects() self.Owner:FireBullets( bullet ) self:EmitSound(Sound(self.Primary.Sound)) self.Owner:ViewPunch( Angle( rnda,rndb,rnda ) ) self:TakePrimaryAmmo(self.Primary.TakeAmmo) self:SetNextPrimaryFire( CurTime() + self.Primary.Delay ) self:SetNextSecondaryFire( CurTime() + self.Primary.Delay ) end function SWEP:SecondaryAttack() end function SWEP:Reload() self:SetIronsights(false) if self.ReloadingTime and CurTime() <= self.ReloadingTime then return end if ( self:Clip1() < self.Primary.ClipSize and self.Owner:GetAmmoCount( self.Primary.Ammo ) > 0 ) then self:DefaultReload( ACT_VM_RELOAD ) local AnimationTime = self.Owner:GetViewModel():SequenceDuration() self.ReloadingTime = CurTime() + AnimationTime self:SetNextPrimaryFire(CurTime() + AnimationTime) self:SetNextSecondaryFire(CurTime() + AnimationTime) self.Weapon:EmitSound("Weapon_SMG1.Reload") end end [editline]25th March 2015[/editline] local IRONSIGHT_TIME = 0.25 function SWEP:GetViewModelPosition( pos, ang ) if ( !self.IronSightsPos ) then return pos, ang end local bIron = self.Weapon:GetNetworkedBool( "Ironsights" ) if ( bIron != self.bLastIron ) then self.bLastIron = bIron self.fIronTime = CurTime() if ( bIron ) then self.SwayScale = 0.3 self.BobScale = 0.1 else self.SwayScale = 1.0 self.BobScale = 1.0 end end local fIronTime = self.fIronTime or 0 if ( !bIron && fIronTime < CurTime() - IRONSIGHT_TIME ) then return pos, ang end local Mul = 1.0 if ( fIronTime > CurTime() - IRONSIGHT_TIME ) then Mul = math.Clamp( (CurTime() - fIronTime) / IRONSIGHT_TIME, 0, 1 ) if (!bIron) then Mul = 1 - Mul end end local Offset = self.IronSightsPos if ( self.IronSightsAng ) then ang = ang * 1 ang:RotateAroundAxis( ang:Right(), self.IronSightsAng.x * Mul ) ang:RotateAroundAxis( ang:Up(), self.IronSightsAng.y * Mul ) ang:RotateAroundAxis( ang:Forward(), self.IronSightsAng.z * Mul ) end local Right = ang:Right() local Up = ang:Up() local Forward = ang:Forward() pos = pos + Offset.x * Right * Mul pos = pos + Offset.y * Forward * Mul pos = pos + Offset.z * Up * Mul return pos, ang end /*--------------------------------------------------------- SetIronsights ---------------------------------------------------------*/ function SWEP:SetIronsights( b ) self.Weapon:SetNetworkedBool( "Ironsights", b ) end [editline]25th March 2015[/editline] SWEP.NextSecondaryAttack = 0 /*--------------------------------------------------------- SecondaryAttack ---------------------------------------------------------*/ function SWEP:SecondaryAttack() if ( !self.IronSightsPos ) then return end if ( self.NextSecondaryAttack > CurTime() ) then return end bIronsights = !self.Weapon:GetNetworkedBool( "Ironsights", false ) self:SetIronsights( bIronsights ) self.NextSecondaryAttack = CurTime() + 0.3 end /*--------------------------------------------------------- onRestore Loaded a saved game (or changelevel) ---------------------------------------------------------*/ function SWEP:OnRestore() self.NextSecondaryAttack = 0 self:SetIronsights( false ) end SWEP.Primary.Cone = 0.02 SWEP.IronSightsPos = Vector(-6.441, -5.428, 1) SWEP.IronSightsAng = Vector(0, 0, 0) /******************************************************** SWEP Construction Kit base code Created by Clavus Available for public use, thread at: facepunch.com/threads/1032378 DESCRIPTION: This script is meant for experienced scripters that KNOW WHAT THEY ARE DOING. Don't come to me with basic Lua questions. Just copy into your SWEP or SWEP base of choice and merge with your own code. The SWEP.VElements, SWEP.WElements and SWEP.ViewModelBoneMods tables are all optional and only have to be visible to the client. ********************************************************/ [editline]25th March 2015[/editline] function SWEP:Initialize() // other initialize code goes here util.PrecacheSound(self.Primary.Sound) util.PrecacheSound(self.ReloadSound) self:SetWeaponHoldType( self.HoldType ) if CLIENT then // Create a new table for every weapon instance self.VElements = table.FullCopy( self.VElements ) self.WElements = table.FullCopy( self.WElements ) self.ViewModelBoneMods = table.FullCopy( self.ViewModelBoneMods ) self:CreateModels(self.VElements) // create viewmodels self:CreateModels(self.WElements) // create worldmodels // init view mode
Please use [code] tags.
[QUOTE=code_gs;47391769]Please use [code] tags.[/QUOTE] I tried. But after that, captcha wanted me to type letters again and again. Even after I put in the right letters.
KeyPress and KeyRelease don't always fire ( unless that was fixed ) if other keys are held. Use think with a controlled-toggle: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/controlled_toggles.lua.html[/url] Example 2 is similar to the controlled toggle you'll want but you'll want to make the hold-types dynamic and instead of hold-type you can look at a var such as self.IsAiming... If you do this, it'll be shared, it won't be clunky, and the logic will only execute once per keypress and once per key release ( mouse 2 )
[QUOTE=Acecool;47397575]KeyPress and KeyRelease don't always fire ( unless that was fixed ) if other keys are held. Use think with a controlled-toggle: [url]https://dl.dropboxusercontent.com/u/26074909/tutoring/logic/controlled_toggles.lua.html[/url] Example 2 is similar to the controlled toggle you'll want but you'll want to make the hold-types dynamic and instead of hold-type you can look at a var such as self.IsAiming... If you do this, it'll be shared, it won't be clunky, and the logic will only execute once per keypress and once per key release ( mouse 2 )[/QUOTE] I've never had problems with them not firing. I'm pretty sure they just use C++ key listeners, so unless there's a problem with that library, idk
[QUOTE=code_gs;47397607]I've never had problems with them not firing. I'm pretty sure they just use C++ key listeners, so unless there's a problem with that library, idk[/QUOTE] I'll re-test them then; I had issues when holding one or more keys and pressing another where the another one didn't report. I found the old git: [url]https://github.com/Facepunch/garrysmod-issues/issues/645[/url] I'm pretty sure it was also the KeyPress / KeyRelease but I'll definitely re-test and post in a bit.
[QUOTE=Acecool;47397678]I'll re-test them then; I had issues when holding one or more keys and pressing another where the another one didn't report. I found the old git: [url]https://github.com/Facepunch/garrysmod-issues/issues/645[/url] I'm pretty sure it was also the KeyPress / KeyRelease but I'll definitely re-test and post in a bit.[/QUOTE] I actually have the same issue, but it's with my keyboard. Make sure you have a seperate key logger open when you test it.
[QUOTE=code_gs;47397706]I actually have the same issue, but it's with my keyboard. Make sure you have a seperate key logger open when you test it.[/QUOTE] But, the thing is the action gets performed, it just doesn't fire the hook. USB Keyboard, sticky-keys etc all disabled.. I'll try with a separate logger.
Sorry, you need to Log In to post a reply to this thread.