Small issue with inverting player movement and ladders
8 replies, posted
For inverting player movement, I use this:
[lua]hook.Add( "SetupMove", "whatever", function( ply, mv, cmd )
mv:SetForwardSpeed( -mv:GetForwardSpeed() ) -- Reverse the movement
mv:SetSideSpeed( -mv:GetSideSpeed() ) -- Reverse the movement
end )[/lua]
Problem is, ladders don't work with this code, so I tried changing player input with this:
[lua]hook.Add("StartCommand", "whatever", function( ply, cmd )
local fr, sd = cmd:GetForwardMove(), cmd:GetSideMove()
if cmd:KeyDown( IN_FORWARD ) then -- If the player is walking forward
cmd:RemoveKey( IN_FORWARD ) -- Make us think he's not going forward
cmd:AddKeys( IN_BACK ) -- Make us think he's going backwards
cmd:SetForwardMove( -1 * math.abs(fr) ) -- Make him walk backwards
end
-- Do the same for all other directions
if cmd:KeyDown( IN_BACK ) then
cmd:RemoveKey( IN_BACK )
cmd:AddKeys( IN_FORWARD )
cmd:SetForwardMove( math.abs(fr) )
end
if cmd:KeyDown( IN_MOVERIGHT ) then
cmd:RemoveKey( IN_MOVERIGHT )
cmd:AddKeys( IN_MOVELEFT )
cmd:SetSideMove( math.abs(sd) )
end
if cmd:KeyDown( IN_MOVELEFT ) then
cmd:RemoveKey( IN_MOVELEFT )
cmd:AddKeys( IN_MOVERIGHT )
cmd:SetSideMove( -1 * math.abs(sd) )
end
end )[/lua]
Problem is, If I walk forward, I reverse the movement so I'm walking backwards, then the movement is reversed again and I just end up walking forward.
Basically whatever part of code I place earlier won't work.
I'm currently using a weird mix of the 2, so it actually reverses your movement, but on ladders, Forward and Right are still not inverted (Because of that reason ^)
[lua]hook.Add("StartCommand", "whatever1", function( ply, cmd )
if cmd:KeyDown( IN_FORWARD ) then -- If the player is walking forward
cmd:RemoveKey( IN_FORWARD ) -- Make us think he's not going forward
cmd:AddKeys( IN_BACK ) -- Make us think he's going backwards
end
-- Do the same for all other directions
if cmd:KeyDown( IN_BACK ) then
cmd:RemoveKey( IN_BACK )
cmd:AddKeys( IN_FORWARD )
end
if cmd:KeyDown( IN_MOVERIGHT ) then
cmd:RemoveKey( IN_MOVERIGHT )
cmd:AddKeys( IN_MOVELEFT )
end
if cmd:KeyDown( IN_MOVELEFT ) then
cmd:RemoveKey( IN_MOVELEFT )
cmd:AddKeys( IN_MOVERIGHT )
end
end )
hook.Add( "SetupMove", "whatever2", function( ply, mv, cmd )
mv:SetForwardSpeed( -mv:GetForwardSpeed() ) -- Reverse the movement
mv:SetSideSpeed( -mv:GetSideSpeed() ) -- Reverse the movement
end )[/lua]
Also this is the AddKeys function [lua]local CUserCmd = FindMetaTable( "CUserCmd" )
function CUserCmd:AddKeys( keys )
local newbuttons = bit.bor( self:GetButtons(), keys )
self:SetButtons( newbuttons )
end[/lua]
I believe if you use CreateMove instead with the keys solution, you won't have to set any Forward/Side move speeds since they'll be updated after they're sent to the server. Untested, though.
[QUOTE=code_gs;52230493]I believe if you use CreateMove instead with the keys solution, you won't have to set any Forward/Side move speeds since they'll be updated after they're sent to the server. Untested, though.[/QUOTE]
The same thing happens :/
Code I used
[lua]if CLIENT then
hook.Add( "CreateMove", "ldkhflkalkghalkrhairogfiovbns", function( cmd )
if cmd:KeyDown( IN_FORWARD ) then -- If the player is walking forward
cmd:RemoveKey( IN_FORWARD ) -- Make us think he's not going forward
cmd:AddKeys( IN_BACK )
end
-- Do the same for all other directions
if cmd:KeyDown( IN_BACK ) then
cmd:RemoveKey( IN_BACK )
cmd:AddKeys( IN_FORWARD )
end
if cmd:KeyDown( IN_MOVERIGHT ) then
cmd:RemoveKey( IN_MOVERIGHT )
cmd:AddKeys( IN_MOVELEFT )
end
if cmd:KeyDown( IN_MOVELEFT ) then
cmd:RemoveKey( IN_MOVELEFT )
cmd:AddKeys( IN_MOVERIGHT )
end
end )
end[/lua]
Well your problem is that you're checking IN_BACK after you have removed IN_FORWARD. You should store it's KeyDown state before you add or remove any key.
[QUOTE=code_gs;52230511]Well your problem is that you're checking IN_BACK after you have removed IN_FORWARD. You should store it's KeyDown state before you add or remove any key.[/QUOTE]
I tried to do it, but for some reason it just didn't work.
[lua]hook.Add("StartCommand", "whatever1", function( ply, cmd )
local frA, bkA, lfA, rtA
if cmd:KeyDown( IN_FORWARD ) then -- If the player is walking forward
cmd:RemoveKey( IN_FORWARD ) -- Make us think he's not going forward
-- cmd:AddKeys( IN_BACK )
bkA = true
end
-- Do the same for all other directions
if cmd:KeyDown( IN_BACK ) then
cmd:RemoveKey( IN_BACK )
-- cmd:AddKeys( IN_FORWARD )
frA = true
end
if cmd:KeyDown( IN_MOVERIGHT ) then
cmd:RemoveKey( IN_MOVERIGHT )
-- cmd:AddKeys( IN_MOVELEFT )
lfA = true
end
if cmd:KeyDown( IN_MOVELEFT ) then
cmd:RemoveKey( IN_MOVELEFT )
-- cmd:AddKeys( IN_MOVERIGHT )
rtA = true
end
if bkA then cmd:AddKeys( IN_BACK ) end
if frA then cmd:AddKeys( IN_FORWARD ) end
if lfA then cmd:AddKeys( IN_MOVELEFT ) end
if rtA then cmd:AddKeys( IN_MOVERIGHT ) end
if cmd:GetButtons() ~= 0 then print( cmd:GetButtons() ) end
end )[/lua]
As for the print part, this was the output:
[T]https://i.imgur.com/bHbuzPG.png[/T]
So the server still thinks I'm walking backwards?
Make sure you're still doing all of this in CreateMove and you have no other hooks touching the keys.
[QUOTE=code_gs;52230595]Make sure you're still doing all of this in CreateMove and you have no other hooks touching the keys.[/QUOTE]
It works!
Any idea why it worked with CreateMove but not with StartCommand?
Final code:
[lua]hook.Add("CreateMove", "whatever1", function( cmd )
local fr, sd = cmd:GetForwardMove(), cmd:GetSideMove()
local frA, bkA, lfA, rtA
if cmd:KeyDown( IN_FORWARD ) then -- If the player is walking forward
cmd:RemoveKey( IN_FORWARD ) -- Make us think he's not going forward
-- cmd:AddKeys( IN_BACK )
cmd:SetForwardMove( -math.abs(fr) )
bkA = true
end
-- Do the same for all other directions
if cmd:KeyDown( IN_BACK ) then
cmd:RemoveKey( IN_BACK )
-- cmd:AddKeys( IN_FORWARD )
cmd:SetForwardMove( math.abs(fr) )
frA = true
end
if cmd:KeyDown( IN_MOVERIGHT ) then
cmd:RemoveKey( IN_MOVERIGHT )
-- cmd:AddKeys( IN_MOVELEFT )
cmd:SetSideMove( -math.abs(sd) )
lfA = true
end
if cmd:KeyDown( IN_MOVELEFT ) then
cmd:RemoveKey( IN_MOVELEFT )
-- cmd:AddKeys( IN_MOVERIGHT )
cmd:SetSideMove( math.abs(sd) )
rtA = true
end
if bkA then cmd:AddKeys( IN_BACK ) end
if frA then cmd:AddKeys( IN_FORWARD ) end
if lfA then cmd:AddKeys( IN_MOVELEFT ) end
if rtA then cmd:AddKeys( IN_MOVERIGHT ) end
end )[/lua]
You can remove the SetForwardMove calls, they aren't needed since they're overrided right after and your logic is improper, anyway. CreateMove works because it is before the command is networked to the server and the move velocities are updated.
If I remove the SetForwardMove calls I walk normally (Except ladders stop working because I change the cmd)
Sorry, you need to Log In to post a reply to this thread.