Why does this work?

I’m just a normal gmod player trying to learn how to lua.

Just to see if I can do it I have an end goal of making a bhop gamemode (don’t really care if it sucks that’s just my goal, just to see if I can do it)

If I can’t figure out how to do it then I find existing code for it and try to figure why it works.

This is autohop. I don’t wanna just copy/paste code so if someone could explain why this works it would be greatly appreciated.


hook.Add( 'SetupMove', 'ss bhop', function( ply, move )
if not ply:IsOnGround() then
move:SetButtons( bit.band( move:GetButtons(), bit.bnot( IN_JUMP ) ) )
end
end )


I hope this is in the right section, if not im sorry.

If I’m correct (not very strong with bitwise), once the player has jumped, it checks that he is not on the ground. If he isn’t, it will retain all keys pressed down, but reset the jump key, thus allowing the user to jump again.

SetupMove -> http://wiki.garrysmod.com/page/GM/SetupMove

as you can see here -> http://wiki.garrysmod.com/page/Enums/IN
IN_JUMP is the second bit of the 24 bit IN
which looks like:
000000000000000000000010
the reason each IN number is a power of two is because you can add them together and always know exactly which buttons are being pressed.
for example if IN_JUMP and IN_ATTACK are being pressed you have:
000000000000000000000011

you can think of it as a big switchboard.

if we -binary not- (bnot) that will turn the bit off.
if we -binary and- (band) that with GetButtons it will keep all other bits unchanged except disable the IN_JUMP bit

so for example, a binary and between
00110 and
01100 will produce
00100

the reason it works is because if you hold down the jump key, the IN_JUMP bit will ONLY be enabled if the player is on the ground

it’s a pretty sweet piece of code…