Nullify player input / cancel its effect

Ok, I’m making a 2d sidescroller. The movement controls work great, except when the player holds w or s (which are forward/back in the first person perspective, but from the 2d sidescroller perspective it’s up/down) they move to the right/left. Is there a function where I can cancel the player’s key presses? If not, what is another way I can do it?

As far as I know, there isn’t a function specifically for it, but I did find a way to do this for an earlier gamemode that I worked on.

Unfortunately, I’m not at my home computer, so I can’t help you.

It involves the OnBindPressed hook, though (clientside). If you don’t get it figured out by the time I get home (in about two hours), then I’ll post what I used.

:buddy:

-snip-

I overcomplicated things.

Returning in PlayerBindPress does the job.

thanks, i’ll try playerbindpress

**[Player.Lock

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.Lock)**

:science:

[editline]04:06PM[/editline]

Except then you’d probably need to animate the character yourself.

That works for stopping the player’s forward/backward movement, but I still need to execute some code when the player presses +back! How would I do that then?

Try **[Gamemode.KeyPress

http://wiki.garrysmod.com/favicon.ico](wiki.garrysmod.com/?title=Gamemode.KeyPress)**.

[editline]04:22PM[/editline]

And **[Player.KeyDown

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Player.KeyDown)**.

I’m not going to lock the player when they hit down. Wouldn’t that cancel all sideways movement?

I used FlapJack’s function, not your’s. Tried executing code before returning in that function, but nothing worked.

**[Gamemode.CreateMove

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=Gamemode.CreateMove)**

And http://wiki.garrysmod.com/?title=CUserCmd

Example: **[CUserCmd.SetButtons

http://wiki.garrysmod.com/favicon.ico](http://wiki.garrysmod.com/?title=CUserCmd.SetButtons)**

These should fit your need perfectly.

Here. This worked for me.

Note that the circumstances I used this in was that the player was locked (via Player:Lock()). I was using it to disable shooting whilst remote-controlling a turret.

[lua]nBindParse = 0
PilotBindCommand = {
{“use”,"~ccgm~_pilot_use"},
{“attack”,"~ccgm~_pilot_attack"}
}
– Bind stuff (for the turrets and such)
function CCGM_BindPress(user,bind,press)
if user.Piloting then
for k,t in pairs(PilotBindCommand) do
if string.find(bind,t[1]) then
local down = “0”
if press then down = “1” end – If the binds are restricted, such as when piloting, the “press” returns true when the button is pressed down, and false when the button is released.
nBindParse = CurTime() + 0.1

			RunConsoleCommand(t[2],down)
		end
	end
	return true
end

end
hook.Add(“PlayerBindPress”,“CCGM Player Bind Press”,CCGM_BindPress)[/lua]

Adjust to fit your needs as required. It appears that returning anything means that the bind won’t execute (judging from this code; it’s been a while since I did it).

This.

How does SetButtons work? I read the wiki page and don’t understand what the “|” and “&” symbols mean. I know that || and && are or/and. But I don’t know what they mean individually.

They are bitwise operators, they act on the individual bits of the operands.

& is a bitwise AND. It performs a logical and on each bit, for example:


10010010101
01010111010 &
-----------
00010010000

| is a bitwise OR. It performs a logical or on each bit, for example:


10010010101
01010111010 |
-----------
11010111111

FFFFFFF, :ninja:. That’s what I get for looking things up :(.

| and & are bitwise operators ( as are << and >> ).

| Is BOR, bitwise or, which performs a logical or on both bits, effectively adding them together.
& is BAND, bitwise and, which pefrorms a logical and on both bits, a “mask” if you will.

If you are making a convar or setting npc capabilities, you would use |:



CreateConVar( "somecvar", 1, FCVAR_NOTIFY | FCVAR_REPLICATE | FCVAR_SOMEVAR )


Which is pretty much the same as:



CreateConVar( "somecvar", 1, FCVAR_NOTIFY + FCVAR_REPLICATE + FCVAR_SOMEVAR )


You would use & when you want to check if a bit is inside of another bit:



if pl:GetFlags( ) & FLAG_POLICE == FLAG_POLICE then
  pl:Give( "weapon_arreststick" )
end


Of course, pl:GetFlags( ) and FLAG_POLICE are not defined but are just methods used in this example.

In plain code, this would be something like:



num % (bit + bit) >= bit


Hope that helps :).

Technically, that is wrong. Bitwise OR performs a logical OR on each bit in a series of bits, it only only has the effect of adding the series of bits together when both series don’t have any of the same bits set. This is why enumerations are only ever powers of two, because each power of two when represented in binary is just one bit set.


1   = 00000001
2   = 00000010
4   = 00000100
8   = 00001000
16  = 00010000
32  = 00100000
64  = 01000000
128 = 10000000
etc

True, will fix that.

What if I just did:

[lua]
if LocalPlayer():KeyDown( IN_FORWARD ) then
{====Code to execute after pressing forward goes here====}
cmd:SetButtons(cmd:GetButtons() - IN_FORWARD)
end
–Will this cancel out the player’s forward movement?
–And if it does will the code above it still execute?
–And if it doesn’t, how would I make it so it does execute while still cancelling movement?
[/lua]

Any help would be great. The thing is I want the player to be able to dig down while pressing IN_BACK and jump when they press IN_FORWARD, but I don’t want the player to move left/right. I tried several methods and none worked. Any code you can provide would be nice :wink: