• Faster Attack Speed on Stunstick
    12 replies, posted
I'm in the progress of balancing my gamemode, and as part of that, I want the stunstick to swing faster. However since the stunstick is just "weapon_stunstick", I can't edit the code in the weapon itself. However I've used a hook to check the the in_attack key is pressed, which checks for the weapon and applies the correct swing speed. It works fine if you press M1 in the correct timing, making you attack pretty fast. But if you hold down M1, it does the first attack and then the second attack with the set delay, but the third attack and onward are on normal delay. I think the cause is that the hook only gets called once when the key is pressed, not spammed as the key is held down. Code: [CODE]hook.Add("KeyPress", "StunstickSpeed", function(ply, key) print("lol") local plyWep = ply:GetActiveWeapon() if plyWep:GetClass() != "weapon_stunstick" then return end if IsValid(ply) and key == IN_ATTACK then print("so far so good") if CurTime() < plyWep:GetNextPrimaryFire() then return end if IsValid( jail ) then print("Jail exists") local jaildistance = ply:GetPos():Distance( jail:GetPos() ) if jaildistance < 1000 then plyWep:SetNextPrimaryFire( CurTime() + 1) print("Setting normal attack speed") else plyWep:SetNextPrimaryFire( CurTime() + 0.2 ) print("Fast attack") end else return end end end)[/CODE] All the printing is for debugging. The objective is to make the stunstick swing faster when you are further away from jail. As stated above, the new attack speed doesn't work when the button is held down. Does anyone know how to make it do that?
Update the PlaybackRate to the animation moves quicker. KeyPress would be fired when the key is pressed ( not held if I recall correctly ); for it to continue while the key is held you could call a timer.Simple to fire at the end of the animation ( GetSequenceDuration ) to reset itself / to call the KeyPress hook ( better to call the function you have associated with it instead of the hook ) to let it go through the sequence again of calculating the new playback rate, updating it, etc.... It would be easy to turn the stunstick into a swep and have the features built into the swep.
[QUOTE=Acecool;45624196]Update the PlaybackRate to the animation moves quicker. KeyPress would be fired when the key is pressed ( not held if I recall correctly ); for it to continue while the key is held you could call a timer.Simple to fire at the end of the animation ( GetSequenceDuration ) to reset itself / to call the KeyPress hook ( better to call the function you have associated with it instead of the hook ) to let it go through the sequence again of calculating the new playback rate, updating it, etc.... It would be easy to turn the stunstick into a swep and have the features built into the swep.[/QUOTE] Alright, many thanks! I made the new function and it runs in the correct fashion, but the playback animation doesn't update its playback rate and the SequenceDuration is always the same. I probably did something wrong, but all the wikis aren't really helpful. Can you see what is wrong here? [CODE]function StunstickSwingSpeed(ply, key) if SERVER then --print(ply:Nick().." attacked with "..key) local plyWep = ply:GetActiveWeapon() --if !IsValid(plyWep) then return end if plyWep:GetClass() != "weapon_stunstick" then return end --print(plyWep:GetClass()) if IsValid(ply) and key == IN_ATTACK then if CurTime() < plyWep:GetNextPrimaryFire() then print("Pressed attack before an attack could be performed") return end print("Player can attack") if IsValid( jail ) then local jaildistance = ply:GetPos():Distance( jail:GetPos() ) print(jaildistance) if jaildistance <= 1000 then plyWep:SetPlaybackRate(1.0) plyWep:SetNextPrimaryFire( CurTime() + plyWep:SequenceDuration() ) print("Setting playback to 1.0, length of animation: "..plyWep:SequenceDuration() ) elseif jaildistance >= 2000 then plyWep:SetPlaybackRate(2.0) plyWep:SetNextPrimaryFire( CurTime() + plyWep:SequenceDuration() ) print("Setting playback to 2.0, length of animation: "..plyWep:SequenceDuration() ) else plyWep:SetPlaybackRate( jaildistance / 1000 ) plyWep:SetNextPrimaryFire( CurTime() + plyWep:SequenceDuration() ) print("Setting playback to ".. jaildistance/1000 ..", length of animation: "..plyWep:SequenceDuration() ) end else plyWep:SetPlaybackRate( 2.0 ) plyWep:SetNextPrimaryFire( CurTime() + plyWep:SequenceDuration() ) print("Setting playback to 2.0, length of animation: "..plyWep:SequenceDuration() ) end end timer.Simple( plyWep:SequenceDuration(), StunstickSwingSpeed) print("End of function reached") end end hook.Add("KeyPress", "StunstickSpeed", StunstickSwingSpeed)[/CODE] It always outputs the length of the animation to be 3.333333. It also sometimes doesn't display an animation at all and only adds the electric sparks that the Stunstick has, but no swinging.
I can't recall if you need to set the play-back rate before or after you start the animation. Try both and see if it changes?
[QUOTE=Acecool;45638827]I can't recall if you need to set the play-back rate before or after you start the animation. Try both and see if it changes?[/QUOTE] This section of code is in the shared.lua, not in the weapon itself, as the weapon is the base weapon_stunstick. I can't edit that weapon itself in the gamemode alone (not that I know of), and the code is very entangled, making it a hassle to make a new weapon and edit all references, although I could still do that if that's the only possible way. And when I tried, it looked all wierd too. Maybe there's an exact lua coded copy I could use? Anyways, I tried adding [CODE]plyWep:SendWeaponAnim( ACT_VM_PRIMARYATTACK )[/CODE] both before and after the playback rate was set, no difference in that regard.
You need to call it on the player's viewmodel, not the weapon.
[QUOTE=Kogitsune;45639802]You need to call it on the player's viewmodel, not the weapon.[/QUOTE] Did that too, it just said that the GetViewModel() method was nil.
Player:GetViewModel exists - you weren't calling it on the weapon, were you?
[QUOTE=Kogitsune;45640515]Player:GetViewModel exists - you weren't calling it on the weapon, were you?[/QUOTE] Isn't the viewmodel clientside?
No, exists in both states.
[QUOTE=Kogitsune;45640515]Player:GetViewModel exists - you weren't calling it on the weapon, were you?[/QUOTE] I tried again, it didn't error on me, but the numbers didn't change. The animation speed did not have any notable difference either. Half the attacks were also still without animation at all. I tried setting the speed to both 10.0 and 0.01, no notable difference. This is how it looked: [CODE]ply:GetViewModel():SetPlaybackRate(10.0)[/CODE]
Oh, and another thing; if I enable the timer at the bottom, which simply calls the function again, [I]ply[/I] isn't brought over, and thus [I]ply:GetActiveWeapon[/I] is nil. How can I otherwise recall this function if it is held down?
It's still a bit problematic. Half the times the animation doesn't even play. The next primary fire seems to work, but if I press a click before it is ready again, the next primary fire is sort of reset to the default. I think the reason why the number doesn't change is because of the electric sparks that appears. They seem to last those 3.3333 seconds. So can anyone help here? Or does someone know of a Lua coded exact copy so I can modify the swep instead of a keypress hook? Every time I tried to copy it myself, its always had a really wierd viewmodel and no animations D:
Sorry, you need to Log In to post a reply to this thread.