1. Post #1
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    Hi, what is the problem? I made a console command to show a vgui, but it wont show up.

    init.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")

    shared.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

    cl_init.lua

    include( "shared.lua" )
    

  2. Post #2
    Emz
    Gold Member
    Emz's Avatar
    October 2008
    2,318 Posts
    Try the derma ones instead:

    vgui.Create( "DFrame" );

    vgui.Create( "DButton" );

  3. Post #3
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    You can't execute the 'bind' command from lua.

  4. Post #4
    Emz
    Gold Member
    Emz's Avatar
    October 2008
    2,318 Posts
    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

  5. Post #5
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    You can't execute the 'bind' command from lua.
    So what would the code for it be?

  6. Post #6
    Taught by John Lua
    MakeR's Avatar
    May 2007
    2,914 Posts
    So what would the code for it be?
    You have to bind it in-game.

  7. Post #7
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    Isnt there another way in which I can execute a function when a key is pressed?

  8. Post #8
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    It's not that simple. You need to use the 'input' library and check for input in a think hook.

  9. Post #9
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    Can you help me more in depth?

  10. Post #10
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    The only way to detect keys that aren't in the IN_KEYS (http://wiki.garrysmod.com/?title=IN_KEYS) would be to use input.IsKeyDown, like so:

    hook.Add("Think","CheckKeys",function()
    	if input.IsKeyDown(65) then LocalPlayer():ChatPrint("You pressed space!") end
    end)

    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:

    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)

  11. Post #11
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    I tried doing this
    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 )

    but it seems like the function doesn't get executed

  12. Post #12
    Emz
    Gold Member
    Emz's Avatar
    October 2008
    2,318 Posts
    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.)

  13. Post #13
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    KeyPress is a completely different hook. You can't use it to detect keyboard keys.

  14. Post #14
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    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.)
    "m has been pressed" is not shown either, so probably the whole function isnt executed

    Edited:

    @nevec, isnt it made to detect when a key is down? i read it on the wiki
    http://wiki.garrysmod.com/?title=Gamemode.KeyPress

  15. Post #15
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    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.

  16. Post #16
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    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)

  17. Post #17
    Gold Member
    Entoros's Avatar
    October 2008
    2,318 Posts
    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, these hooks do not detect any other keys, which is pretty much the whole keyboard and numpad. 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 input.IsKeyDown().

    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.

  18. Post #18
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    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 );
    

    A bit simplified version.

  19. Post #19
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    ~= True means all but True?

    Edited:

    or not true?

  20. Post #20
    Gold Member
    Nevec's Avatar
    June 2008
    1,092 Posts
    ~= = != = not equal

  21. Post #21
    Gold Member
    TerabyteS's Avatar
    June 2009
    2,997 Posts
    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