• Disable key use (temporarily) for certain keys
    22 replies, posted
For a code I am making, I need to be able to make it to where if the user presses w or s, nothing will happens (disables input for those keys), how would i do this (keep in mind I want to be able to re-enable it later)?
Use the 'SetupMove' hook. bitwise not the button(s) and bitwise and it with CMoveData:GetButtons().
what does GetButtons return if the are holding more than 1 button?
A binary sum of all held keys. Correct me if I am wrong.
so how would I be able to use that if the player is holding more more keys than just A and W?
Using bit. library from LuaJIT.
is that still built into gmod? (I am stupid and don't keep up with these things)
[QUOTE=jack10685;42232498]is that still built into gmod? (I am stupid and don't keep up with these things)[/QUOTE] Yes. [url]http://wiki.garrysmod.com/page/Category:bit[/url]
[QUOTE=jack10685;42232418]so how would I be able to use that if the player is holding more more keys than just A and W?[/QUOTE] If you wanted to make it so W did nothing at all, you would do [lua] cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); [/lua] I recommend that you read up on bitwise a little to understand the basics of bitwise.
Do you really need this bitwise stuff? I always just subtract the value of the KEY_x from the stack. So you couldn't you just: [lua] cmd:SetButtons(cmd:GetButtons() - <value of the KEY enum>) [/lua] ? [url]http://wiki.garrysmod.com/page/Enums/KEY[/url]
[QUOTE=LennyPenny;42235597]Do you really need this bitwise stuff? I always just subtract the value of the KEY_x from the stack. So you couldn't you just: [lua] cmd:SetButtons(cmd:GetButtons() - <value of the KEY enum>) [/lua] ? [url]http://wiki.garrysmod.com/page/Enums/KEY[/url][/QUOTE] Although bitwise or is basically the same as the sum, it's not interchangeable. If the pressed buttons were 0 (for example), you would be setting the buttons to -value, which will give undesired results. [editline]a[/editline] Say you had the flag 12, which is the sum of 4 and 8 (due to how it works, those are the only two numbers that could possibly give that sum), and you wanted to remove 8. The binary values for each of these are as follows: 04: 00000000100 08: 00001001000 12: 10010001100 First you would NOT the value you wish to remove (8) 0110111 Then you would AND it with the flag 00000110111 10010001100 AND is what you might think it is; keeping the values that are the same. What you are left with is 00000000100 Now what would it look like if you tried to remove 8 from 4? [code] NOT 8: 00000110111 4: 00000000100 = 00000000100 [/code]
ok, so if i want to re-enable keys, what would i do? [lua]cmd:SetButtons(bit.band(cmd:GetButtons(), KEY_W));[/lua]? or would i just put it as something that runs if a certain value is equal to something [lua]if var then cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end[/lua]?
[QUOTE=jack10685;42236596]ok, so if i want to re-enable keys, what would i do? [lua]cmd:SetButtons(bit.band(cmd:GetButtons(), KEY_W));[/lua]? or would i just put it as something that runs if a certain value is equal to something [lua]if var then cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end[/lua]?[/QUOTE] If you just need this for disabling keys oia certain condition is met, you might just want to remove/add the SetupMove/CreateMove hook every time the condition is met or ceases to be met. You can look at how I did it in my bhop script: [url]https://github.com/LennyPenny/Lennys/blob/master/lua/Lenny/bhop.lua#L31-L37[/url] (look at the yellow highlighted part)
[QUOTE=LennyPenny;42236711]If you just need this for disabling keys oia certain condition is met, you might just want to remove/add the SetupMove/CreateMove hook every time the condition is met or ceases to be met. You can look at how I did it in my bhop script: [url]https://github.com/LennyPenny/Lennys/blob/master/lua/Lenny/bhop.lua#L31-L37[/url] (look at the yellow highlighted part)[/QUOTE] does that remove the hook for everyone, because i only want it removed for one person.
[QUOTE=jack10685;42236757]does that remove the hook for everyone, because i only want it removed for one person.[/QUOTE] Oh, I assumed you would be running this clientside. If you are running this serverside, you can use the first argument of the CreateMove hook to get the player it's called for. You wouldn't remove the hook then. [lua] hook.Add("CreateMove", "hookname", function(ply, cmd) if ply.ConditionIsMet then cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end end) [/lua]
You need to run it shared so you don't get prediction errors. Instead of removing it, just check if the player the hook is returning is a player that you want to disable the keys on. [QUOTE=jack10685;42236596]ok, so if i want to re-enable keys, what would i do? [lua]cmd:SetButtons(bit.band(cmd:GetButtons(), KEY_W));[/lua]? or would i just put it as something that runs if a certain value is equal to something [lua]if var then cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end[/lua]?[/QUOTE] To re-enable, just don't disable. The client is sending the keys already.
so hows this - [lua] hook.Add( "SetupMove", "allowDrive", function( ply, cmd ) if ply:InVehicle() then if ply:GetVehicle().Fuel <= 0 cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end end end ) [/lua] (this is in server-side)
[URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4d65.html"]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4d65.html[/URL] you guys are overcomplicating it. [code] function GM:PlayerBindPress(ply, bind, pressed) if string.find(bind, "+forward") then return true end if string.find(bind, "+back") then return true end end [/code] ^clientside
[QUOTE=sackcreator54;42237969][URL="http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4d65.html"]http://maurits.tv/data/garrysmod/wiki/wiki.garrysmod.com/index4d65.html[/URL] you guys are overcomplicating it. [code] function GM:PlayerBindPress(ply, bind, pressed) if string.find(bind, "+forward") then return true end if string.find(bind, "+back") then return true end end [/code] ^clientside[/QUOTE] No. If you want to stop a vehicle from moving, then you're better off using this if it's derived from the "prop_vehicle_jeep" class. [lua] Vehicle:Fire("TurnOff", "", 0) [/lua]
[QUOTE=brandonj4;42238036]No. If you want to stop a vehicle from moving, then you're better off using this if it's derived from the "prop_vehicle_jeep" class. [lua] Vehicle:Fire("TurnOff", "", 0) [/lua][/QUOTE] [QUOTE] For a code I am making, [B]I need to be able to make it to where if the user presses w or s, nothing will happens[/B] (disables input for those keys), how would i do this (keep in mind I want to be able to re-enable it later)? [/QUOTE]
[QUOTE=sackcreator54;42238092][/QUOTE] I am assuming you don't understand. He is using this for a vehicle fuel addon, if you would read his post above. [QUOTE=jack10685;42237236]so hows this - [lua] hook.Add( "SetupMove", "allowDrive", function( ply, cmd ) if ply:InVehicle() then if ply:GetVehicle().Fuel <= 0 cmd:SetButtons(bit.band(cmd:GetButtons(), bit.bnot(KEY_W))); end end end ) [/lua] (this is in server-side)[/QUOTE] He wants to make the vehicle stop moving when it has 0 fuel. Jack, use my method if the description matches what you're trying to do.
[QUOTE=brandonj4;42238161]I am assuming you don't understand. He is using this for a vehicle fuel addon, if you would read his post above. He wants to make the vehicle stop moving when it has 0 fuel. Jack, use my method if the description matches what you're trying to do.[/QUOTE] ok, but 2 quick questions. 1. when they re-fuel the car, should i use Vehicle:Fire("TurnOn", "", 0) to allow driving use again? 2. will the driver still be able to turn while coming to a stop? (I want this to be true)
[QUOTE=jack10685;42239653]ok, but 2 quick questions. 1. when they re-fuel the car, should i use Vehicle:Fire("TurnOn", "", 0) to allow driving use again? 2. will the driver still be able to turn while coming to a stop? (I want this to be true)[/QUOTE] From what I remember, "TurnOff" applies the breaks. [editline]a[/editline] [quote=valvewiki] TurnOff Stop engine, disable throttle, engage brakes.[/quote]
Sorry, you need to Log In to post a reply to this thread.