• input.IsKeyDown to work like Player.KeyPressed
    4 replies, posted
I've been playing around with the [url=http://wiki.garrysmod.com/?title=IN_KEYS]KEY_* enums[/url] and [b][url=http://wiki.garrysmod.com/?title=Input.IsKeyDown]input.IsKeyDown[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b], and was wondering how to make it function like [b][url=http://wiki.garrysmod.com/?title=Player.KeyPressed]Player.KeyPressed[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b]. I don't recall where I read this but there is a way to create your own customized keys like this. I want the player, when it is his turn, to be able to move a marker that shows where they can move to (Just like any type of board or chess game). I already have the coding done for the most part, just that whenever input.IsKeyDown is called from the player it keeps on executing the concommand constantly from the sever. I was looking to have it set up like Player.KeyPressed where it only works once each time they press that button. This executes once every time the IN_FORWARD is pressed down. [lua] function Buttons_USED() if LocalPlayer():KeyPressed(IN_FORWARD) then RunConsoleCommand("Move_IND_UP") end end hook.Add("Think", "Buttons_USED", Buttons_USED) [/lua] This executes constantly when KEY_UP is pressed. It still runs a couple or more times when you press it quickly too. [lua] function Buttons_USED() if input.IsKeyDown(KEY_UP) then RunConsoleCommand("Move_IND_UP") end end hook.Add("Think", "Buttons_USED", Buttons_USED) [/lua] The reason why I'm using input.IsKeyDown is because I don't want the players to be able to move freely or attack on their own. They have to be giving permission from the server to do so and have to follow what they can do on their turn. With that, I am using [b][url=http://wiki.garrysmod.com/?title=Gamemode.PlayerBindPress]Gamemode.PlayerBindPress[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b] to bind the IN_* Enums.
Just set up a variable that initializes outside of where you are calling it from and set it to false, put an if statement in checking to see if the variable is false and if it is, run the code and set the variable to true. Then in your key release bit set it back to false. I.e. [lua] local keylock = false function Buttons_USED() if not keylock then if input.IsKeyDown(KEY_UP) then RunConsoleCommand("Move_IND_UP") keylock = true end end end [/lua] This is just an example to show you what I mean, but you need to save the variable per player otherwise it will get called and stored for everyone in the same variable.
[QUOTE=Feihc;28888746]Just set up a variable that initializes outside of where you are calling it from and set it to false, put an if statement in checking to see if the variable is false and if it is, run the code and set the variable to true. Then in your key release bit set it back to false. I.e. [lua] local keylock = false function Buttons_USED() if not keylock then if input.IsKeyDown(KEY_UP) then RunConsoleCommand("Move_IND_UP") keylock = true end end end [/lua] This is just an example to show you what I mean, but you need to save the variable per player otherwise it will get called and stored for everyone in the same variable.[/QUOTE] Would [b][url=http://wiki.garrysmod.com/?title=Player.GetByID]Player.GetByID[img_thumb]http://wiki.garrysmod.com/favicon.ico[/img_thumb][/url][/b] suffice if I create a statement in the PlayerSpawn hook that grants a player their own variables to use??
IIRC, GAMEMODE:Think has a player as an argument, so you can initialize the variables on playerspawn or whatever and check them in think with ply.KeyLock or whatever you name your variable
Sorry, you need to Log In to post a reply to this thread.