• Calling a function on KeyDown
    6 replies, posted
Hello guys I was wondering how to call my HUDPaint function when a player is pressing a key? The IsForwardKeyDown function is working I tested it with chatprint but it doesn't seem to call my function key_press() when you hit W. Any help on this would be greatly appreciated!! ERROR when hitting W: [code] Hook 'wKeyDownExample' Failed: [gamemodes/hidenseek/gamemode/shared.lua:17] attempt to index global 'draw' (a nil value) 1. lua/includes/modules/hook.lua:83 (unknown) [/code] My shared.lua: [lua] function key_press() draw.SimpleText( "TEST", "ScoreboardText", ScrW()/2, ScrH()/2, Color(0,128,255,255), TEXT_ALIGN_LEFT) end hook.Add("HUDPaint", "key_press", key_press) local function wKeyDown( ply ) local cmd = ply:GetCurrentCommand() if cmd:KeyDown( IN_FORWARD ) then --ply:ChatPrint( "TESTING" ) key_press() end end hook.Add( "Move", "wKeyDownExample", wKeyDown ) [/lua]
did you overwrite the draw library? ._.
Nope, and this is in gmod13 I'm not sure if it makes a difference or not.
[lua] local drawshit = false local function key_press() if not drawshit then return end draw.SimpleText( "TEST", "ScoreboardText", ScrW()/2, ScrH()/2, Color(0,128,255,255), TEXT_ALIGN_LEFT) end hook.Add("HUDPaint", "key_press", key_press) local function wKeyDown( ply ) local cmd = ply:GetCurrentCommand() if cmd:KeyDown( IN_FORWARD ) and not drawshit then drawshit = true elseif drawshit then drawshit = false end end hook.Add( "Move", "wKeyDownExample", wKeyDown ) [/lua]
[lua] local pl = LocalPlayer() local function DrawKeyPress( ) if pl:KeyDown( IN_FORWARD ) then draw.SimpleText( "TEST", "ScoreboardText", ScrW()/2, ScrH()/2, Color(0,128,255,255), TEXT_ALIGN_LEFT) end end hook.Add( "HUDPaint", "DrawKeyPress", DrawKeyPress ) [/lua]
Shadow it works tyvm! I see what you did but I am having a hard time following it, the "if not" lines are confusing me, maybe with a fresh mind I can figure it out tomorrow. Feel free to elaborate on it if you don't mind =D
I hope you do know that you're technically adding the same hook server-side since that's the shared.lua file. Which runs on both client and server. I'm pretty sure the server doesn't have the table "draw" since it has no actual interface at all, let alone a screen.
Sorry, you need to Log In to post a reply to this thread.