• Vgui doesn't show up when I press a key
    21 replies, posted
  • Avatar of TerabyteS
  • Hi, what is the problem? I made a console command to show a vgui, but it wont show up. init.lua [lua]AddCSLuaFile("cl_init.lua") AddCSLuaFile("shared.lua") include("shared.lua") function GM:PlayerSpawn( ply ) self.BaseClass:PlayerSpawn( ply ) ply:SetGravity(0.75) ply:SetMaxHealth(100, true) ply:SetWalkSpeed(720) ply:SetRunSpeed(720) end function GM:PlayerInitialSpawn(ply) end function GM:PlayerLoadout(ply) if ply:Team() == 1 then ply:Give("weapon_pistol") elseif ply:Team() == 2 then ply:Give("weapon_pistol") end end function sb_team1(ply) if ply:Team() != 1 then ply:KillSilent() ply:SetTeam(1) ply:Spawn() ply:PrintMessage(HUD_PRINTTALK, ply:Nick().." is now a Combine") end end function sb_team2(ply) if ply:Team() != 2 then ply:KillSilent() ply:SetTeam( 2 ) ply:Spawn() ply:PrintMessage(HUD_PRINTTALK, ply:Nick().." is now a Rebel") end end concommand.Add("show_classchange_menu", classchange) RunConsoleCommand("bind M show_classchange_menu")[/lua] shared.lua [lua]GM.Name = "Half Life 2: Team Fortress" GM.Author = "Tera" GM.Email = "gabrycir@hotmail.it" DeriveGamemode( "base" ) team.SetUp( 1, "Combine", Color( 125, 125, 125, 255 ) ) team.SetUp( 2, "Resistance", Color( 255, 255, 255, 255 ) ) function classchange() if CLIENT then local Frame = vgui.Create("Frame"); Frame:SetSize(200, 200) Frame:SetPos(100, 100) Frame:SetVisible(true) Frame:MakePopup() Frame:PostMessage("SetTitle", "text", "This is the title") local Button = vgui.Create("Button", Frame) Button:SetText("Click me") Button:SetPos(30, 5) Button:SetWide(100) function Button:DoClick() self:SetText("Clicked") local menu123 = DermaMenu() menu123:AddOption("hello", function() Msg("Hello") end ) menu123:AddOption("how", function() Msg("How") end ) menu123:AddOption("are", function() Msg("Are") end ) menu123:AddOption("you", function() Msg("You") end ) menu123:AddOption("?", function() Msg("?") end ) menu123:Open() end end end[/lua] cl_init.lua [lua]include( "shared.lua" ) [/lua]
  • Avatar of Emz
  • Try the derma ones instead: vgui.Create( [b]"DFrame"[/b] ); vgui.Create( [b]"DButton"[/b] );
  • Avatar of Emz
  • Also make the concommand.add in the shared file where the function is created (inside the Client bit.) All you're doing is adding an empty console command as you're adding it on the server and your function starts with if( CLIENT ) so the server sees it as: FunctionName() // Nothing in here as it is all client side. end
  • Avatar of TerabyteS
  • [QUOTE=Nevec;16180189]You can't execute the 'bind' command from lua.[/QUOTE] So what would the code for it be?
  • Avatar of MakeR
  • [QUOTE=TerabyteS;16180569]So what would the code for it be?[/QUOTE] You have to bind it in-game.
  • Avatar of Nev
  • It's not that simple. You need to use the 'input' library and check for input in a think hook.
  • Avatar of Entoros
  • The only way to detect keys that aren't in the IN_KEYS ([url]http://wiki.garrysmod.com/?title=IN_KEYS[/url]) would be to use input.IsKeyDown, like so: [lua]hook.Add("Think","CheckKeys",function() if input.IsKeyDown(65) then LocalPlayer():ChatPrint("You pressed space!") end end)[/lua] However, one problem is that even if you press the key rather quickly, because this hook runs every tick or so it's very likely that you'll get "You pressed space!" 3 or 4 times. For example, I made a function that detects the numpad, so to avoid this issue I did: [lua]local keys = { {38,1}, {39,2}, {40,3}, {41,4}, {42,5}, {43,6}, {44,7}, {45,8}, {46,9}, } local keypressed = false hook.Add("Think","ExpKeys",function() for _,v in ipairs(keys) do if input.IsKeyDown(v[1]) then if keypressed == false then RunConsoleCommand("exp_useskill",v[2]) keypressed = true timer.Destroy("Key") else if not timer.IsTimer("Key") then timer.Create("Key",0.25,1,function() keypressed = false end) end end end end end)[/lua]
  • Avatar of TerabyteS
  • I tried doing this [lua]GM.Name = "Half Life 2: Team Fortress" GM.Author = "Tera" GM.Email = "gabrycir@hotmail.it" DeriveGamemode( "base" ) team.SetUp( 1, "Combine", Color( 125, 125, 125, 255 ) ) team.SetUp( 2, "Resistance", Color( 255, 255, 255, 255 ) ) function classchange() Msg ("M HAS BEEN PRESD") if CLIENT then local Frame = vgui.Create("Frame"); Frame:SetSize(200, 200) Frame:SetPos(100, 100) Frame:SetVisible(true) Frame:MakePopup() Frame:PostMessage("SetTitle", "text", "This is the title") local Button = vgui.Create("Button", Frame) Button:SetText("Click me") Button:SetPos(30, 5) Button:SetWide(100) function Button:DoClick() self:SetText("Clicked") local menu123 = DermaMenu() menu123:AddOption("hello", function() Msg("Hello") end ) menu123:AddOption("how", function() Msg("How") end ) menu123:AddOption("are", function() Msg("Are") end ) menu123:AddOption("you", function() Msg("You") end ) menu123:AddOption("?", function() Msg("?") end ) menu123:Open() end end end function KeyPressed (P, key) if (key == 23) then classchange() end end hook.Add( "KeyPress", "KeyPressedHook", KeyPressed )[/lua] but it seems like the function doesn't get executed
  • Avatar of Emz
  • Question, does the message print to console the "M has been pressed." If that happens but not the stuff in the if( CLIENT ) then please read my earlier reply (my second one.)
  • Avatar of Nev
  • KeyPress is a completely different hook. You can't use it to detect keyboard keys.
  • Avatar of TerabyteS
  • [QUOTE=Emz;16181773]Question, does the message print to console the "M has been pressed." If that happens but not the stuff in the if( CLIENT ) then please read my earlier reply (my second one.)[/QUOTE] "m has been pressed" is not shown either, so probably the whole function isnt executed [editline]08:59PM[/editline] @nevec, isnt it made to detect when a key is down? i read it on the wiki [url]http://wiki.garrysmod.com/?title=Gamemode.KeyPress[/url]
  • Avatar of Nev
  • It detects when a special bind is down - when you press your sprint key, use key, walk key, etc. To actually detect keyboard input, use Entoros' code.
  • Avatar of TerabyteS
  • I don't understand that code, so I don't know where/how to put it. Is someone up for a short explanation? (sorry i come from python, it's similar but different in the basic things like for)
  • Avatar of Entoros
  • Ok, here's how it goes. The following hooks: KeyPress KeyDown KeyRelease These hooks only run when an IN_KEY is pressed. IN_KEYS are various functions for use in the default HL2 keyboard, like walking, firing, and sprinting. However, [b]these hooks do not detect any other keys, which is pretty much the whole keyboard and numpad.[/b] Yet there is a function that can do this, but the unfortunate thing is it can only be called on the client; the answer is [b]input.IsKeyDown()[/b]. Thus, the only way to detect if a key that's not an IN_KEY is to use input.IsKeyDown in a Think hook (think basically just runs a lot). Refer to the code in my previous post on this.
  • Avatar of Nev
  • [lua] local keydown; local function Think( ) if( input.IsKeyDown( KEY_M ) and keydown ~= true ) then -- m key was pressed keydown = true; elseif( keydown == true ) then -- m key was released keydown = false; end end hook.Add( "Think", "CheckKeyInput", Think ); [/lua] A bit simplified version.
  • Avatar of TerabyteS
  • Thanks, nevec. I read the simplified version and understood it (i used similar methods when I made little games with python) I will try and go on with the gamemode and tell you via mp (if its ok for you) if i have any other problems
  • Avatar of Watercycle
  • I'm not trying to revive this thread, but in case anyone else stumbles upon it here is the code you (probably) want. [CODE] -- |-----------------------------------------------------------------| -- -- | CLIENTSIDE KEYBOARD PRESS / RELEASE EVENTS | -- -- |-----------------------------------------------------------------| -- if (SERVER) then return end -- this is a clientside only hook local SPECIAL_KEYS = {KEY_Q, KEY_M} -- which keyboard keys are we listening for? local keysPressedLastFrame = {} -- We have to check whether keys were pressed the previous frame - every frame. hook.Add( "Think", "CheckClientKeyInput", function() for index, keyCode in pairs(SPECIAL_KEYS) do local pressedBefore = keysPressedLastFrame[keyCode] local currentlyPressed = input.IsKeyDown(keyCode) if currentlyPressed and not pressedBefore then hook.Run( "ClientKeyPress", keyCode ) -- call the key press event keysPressedLastFrame[keyCode] = true elseif not currentlyPressed and pressedBefore then hook.Run( "ClientKeyRelease", keyCode ) -- call the key release event keysPressedLastFrame[keyCode] = false end end end) [/CODE] Example Usage: [CODE] function guiClicker( key ) if input.IsKeyDown(KEY_Q) then gui.EnableScreenClicker(true) else gui.EnableScreenClicker(false) end end hook.Add( "ClientKeyPress", "clicker enable", guiClicker ) hook.Add( "ClientKeyRelease", "clicker disable", guiClicker ) [/CODE] See [URL="https://wiki.garrysmod.com/page/Net_Library_Usage"]Net Library[/URL] for letting the server know about these events.