Jump to content

[Question] bind key to gui


kieran

Recommended Posts

I made a simple gui for information etc, trying to bind the F1 key to the gui...  Worked fine when I just tried onClientResourceStart and done nothing with key binds, but no debug errors, so I can't see where I gone wrong.

    helpTabs = {}
    helphelpTabsPan = {}
    helpLabel = {}
    helpButton = {}
    helpWindow = {}
    helpMemo = {}
--helpState is false.
helpState = false

function ShowHelp(key, keyState)
  --Checking if "helpState" is true.
		if ( keyState == "down" ) and helpState == true then
		guiSetVisible(helpWindow, false)
		destroyElement(helpWindow)
		helpWindow = nil
		showCursor(false)
		else 
		if helpState == false then
		guiSetInputEnabled(false)
		showCursor(true)
		--Random GUI crap.
		local screenW, screenH = guiGetScreenSize()
        helpWindow = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
        guiWindowSetSizable(helpWindow, false)
        guiSetAlpha(helpWindow, 0.92)
        guiSetProperty(helpWindow, "CaptionColour", "FFD7E21C")

        helpButton = guiCreateButton(485, 489, 96, 35, "Close", false, helpWindow)
        guiSetProperty(helpButton, "NormalTextColour", "FFFE0000")
        helphelpTabsPan = guiCreateTabPanel(9, 28, 572, 461, false, helpWindow)

        helpTabs = guiCreateTab("Rules", helphelpTabsPan)

        memo = guiCreateMemo(6, 32, 561, 399, "Text goes here.", false, helpTabs)
        label = guiCreateLabel(13, 10, 544, 26, "Label text goes here.", false, helpTabs)
        guiLabelSetColor(label, 255, 100, 25)

        helpTabs2 = guiCreateTab("Commands", helphelpTabsPan)

        memo2 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpTabs2)
        label2 = guiCreateLabel(15, 10, 544, 26, "Label text goes here.", false, helpTabs2)
        guiLabelSetColor(label2, 255, 100, 25)

        helpTabs3 = guiCreateTab("Buttons", helphelpTabsPan)

        memo3 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpTabs3)  
		helpState = true		
		addEventHandler("onClientGUIClick", helpButton,onClickCloseBut)
	end
end
end


--This closes it, but am I setting "helpState" correctly?
function onClickCloseBut(button,state) 
	if(button == "left" and state == "up") then
		if (source == helpButton) then
		guiSetVisible(helpWindow, false)
		destroyElement(helpWindow)
		helpWindow = nil
		showCursor(false)
		helpState = false
		end
	end
end


--KEY BIND.
function bindTheKeys ()
	bindKey ( "F1", "down", ShowHelp )   
end
addEventHandler("onClientPlayerJoin", getRootElement(), bindTheKeys )

Basically this is a lot to read, so top part before guiGetScreenSize and after the GUI stuff is probably where the problem is....  It is obvious but I have never tried binding keys to client before.  Thanks for help!

Link to comment
helpGUI = {}
local screenW, screenH = guiGetScreenSize()

function ShowHelp()
	-- window
	helpGUI.window = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
	guiWindowSetSizable(helpGUI.window, false)
	guiSetAlpha(helpGUI.window, 0.92)
	guiSetProperty(helpGUI.window, "CaptionColour", "FFD7E21C")
	-- close button and tab panel
	helpGUI.close = guiCreateButton(485, 489, 96, 35, "Close", false, helpGUI.window)
	guiSetProperty(helpGUI.close, "NormalTextColour", "FFFE0000")
	helpGUI.tabpanel = guiCreateTabPanel(9, 28, 572, 461, false, helpGUI.window)
	-- Rules tab
	helpGUI.rules = guiCreateTab("Rules", helpGUI.tabpanel)
	helpGUI.memo1 = guiCreateMemo(6, 32, 561, 399, "Text goes here.", false, helpGUI.rules)
	helpGUI.label1 = guiCreateLabel(13, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)
	guiLabelSetColor(helpGUI.label1, 255, 100, 25)
	-- Commands tab
	helpGUI.rules = guiCreateTab("Commands", helpGUI.tabpanel)
	helpGUI.memo2 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.rules)
	helpGUI.label2 = guiCreateLabel(15, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)
	guiLabelSetColor(helpGUI.label2, 255, 100, 25)
	-- Buttons tab? wtf weird name ;-;
	helpGUI.buttons = guiCreateTab("Buttons", helpGUI.tabpanel)
	helpGUI.memo3 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.buttons)  
end
addEventHandler( "onClientResourceStart", resourceRoot, ShowHelp)

function guiClose()
	if source == helpGUI.close then
		guiSetVisible(helpWindow, false)
		showCursor(false)
	end
end
addEventHandler( "onClientGUIClick", root, guiClose)

-- bind if u want it 
function bind()
	guiSetVisible(helpGUI.window, not guiGetVisible(helpGUI.window))
	showCursor(guiGetVisible(helpGUI.window))
end
bindKey("f1", "down", bind)

try this i think it will work @kieran

Edited by kikos500
  • Like 1
Link to comment

what doesn't work and i just cleaned up your code a bit ;( and btw 

   helpTabs = {}
    helphelpTabsPan = {}
    helpLabel = {}
    helpButton = {}
    helpWindow = {}
    helpMemo = {}
why were u adding tables while u r not using them? from what i saw u were replacing them with variables in ur script if u need i can give u a detailed explanation about how tables work 

helpGUI = {}
local screenW, screenH = guiGetScreenSize()

function ShowHelp()
	-- window
	helpGUI.window = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
	guiWindowSetSizable(helpGUI.window, false)
	guiSetAlpha(helpGUI.window, 0.92)
	guiSetProperty(helpGUI.window, "CaptionColour", "FFD7E21C")
	-- close button and tab panel
	helpGUI.close = guiCreateButton(485, 489, 96, 35, "Close", false, helpGUI.window)
	guiSetProperty(helpGUI.close, "NormalTextColour", "FFFE0000")
	helpGUI.tabpanel = guiCreateTabPanel(9, 28, 572, 461, false, helpGUI.window)
	-- Rules tab
	helpGUI.rules = guiCreateTab("Rules", helpGUI.tabpanel)
	helpGUI.memo1 = guiCreateMemo(6, 32, 561, 399, "Text goes here.", false, helpGUI.rules)
	helpGUI.label1 = guiCreateLabel(13, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)
	guiLabelSetColor(helpGUI.label1, 255, 100, 25)
	-- Commands tab
	helpGUI.rules = guiCreateTab("Commands", helpGUI.tabpanel)
	helpGUI.memo2 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.rules)
	helpGUI.label2 = guiCreateLabel(15, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)
	guiLabelSetColor(helpGUI.label2, 255, 100, 25)
	-- Buttons tab? wtf weird name ;-;
	helpGUI.buttons = guiCreateTab("Buttons", helpGUI.tabpanel)
	helpGUI.memo3 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.buttons)  
end
addEventHandler( "onClientResourceStart", resourceRoot, ShowHelp)

function guiClose()
	guiSetVisible(helpWindow, false)
	showCursor(false)
end
addEventHandler("onClientGUIClick", helpGUI.close, guiClose)

-- bind if u want it 
function bind()
	guiSetVisible(helpGUI.window, not guiGetVisible(helpGUI.window))
	showCursor(guiGetVisible(helpGUI.window))
end
bindKey("f1", "down", bind)

 

Edited by kikos500
Link to comment
2 hours ago, Gordon_G said:

Then, juste trigger the function bindKey clientside from serverside when the player login

onPlayerLogin triggerClientEvent

Omg I feel so stupid ? I was thinking I needed to make event on it but wasn't too sure, thanks!

 

3 hours ago, kikos500 said:

what doesn't work and i just cleaned up your code a bit ;( and btw 

   helpTabs = {}
    helphelpTabsPan = {}
    helpLabel = {}
    helpButton = {}
    helpWindow = {}
    helpMemo = {}
why were u adding tables while u r not using them? from what i saw u were replacing them with variables in ur script if u need i can give u a detailed explanation about how tables work 


helpGUI = {}local screenW, screenH = guiGetScreenSize()function ShowHelp()	-- window	helpGUI.window = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)	guiWindowSetSizable(helpGUI.window, false)	guiSetAlpha(helpGUI.window, 0.92)	guiSetProperty(helpGUI.window, "CaptionColour", "FFD7E21C")	-- close button and tab panel	helpGUI.close = guiCreateButton(485, 489, 96, 35, "Close", false, helpGUI.window)	guiSetProperty(helpGUI.close, "NormalTextColour", "FFFE0000")	helpGUI.tabpanel = guiCreateTabPanel(9, 28, 572, 461, false, helpGUI.window)	-- Rules tab	helpGUI.rules = guiCreateTab("Rules", helpGUI.tabpanel)	helpGUI.memo1 = guiCreateMemo(6, 32, 561, 399, "Text goes here.", false, helpGUI.rules)	helpGUI.label1 = guiCreateLabel(13, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)	guiLabelSetColor(helpGUI.label1, 255, 100, 25)	-- Commands tab	helpGUI.rules = guiCreateTab("Commands", helpGUI.tabpanel)	helpGUI.memo2 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.rules)	helpGUI.label2 = guiCreateLabel(15, 10, 544, 26, "Label text goes here.", false, helpGUI.rules)	guiLabelSetColor(helpGUI.label2, 255, 100, 25)	-- Buttons tab? wtf weird name ;-;	helpGUI.buttons = guiCreateTab("Buttons", helpGUI.tabpanel)	helpGUI.memo3 = guiCreateMemo(5, 32, 561, 399, "Text goes here.", false, helpGUI.buttons)  endaddEventHandler( "onClientResourceStart", resourceRoot, ShowHelp)function guiClose()	guiSetVisible(helpWindow, false)	showCursor(false)endaddEventHandler("onClientGUIClick", helpGUI.close, guiClose)-- bind if u want it function bind()	guiSetVisible(helpGUI.window, not guiGetVisible(helpGUI.window))	showCursor(guiGetVisible(helpGUI.window))endbindKey("f1", "down", bind)

 

I done it that way because I used GUI editor, it is easier for me as I can just check top to see if I made typos etc.  Also buttons tab is so I my list buttons ;) as I have various binds, I was just struggling to bind GUI's, this is the second one I done, so still learning.

Sorry for quoting, on tablet and it doesn't like mentions 

Edited by kieran
Link to comment
23 minutes ago, Uknown. said:

What do you want being more detailed?

Okay, basically for now only way my GUI works is player logs in, it shows the GUI, then it checks visibility.  The thing is I want to make it so my GUI doesn't have to come up for the script to notice an element is there....  Could I do this by simply binding a key to create gui or is it a little more complicated?  :oops:

Here's the current code (Yes I just kept it as is, easier than changing the original working script.)

Server

function HelpBinds()
	triggerClientEvent (source,"Help_Panel",getRootElement())
	triggerClientEvent (source,"Help_Login",getRootElement())
end
addEventHandler("onPlayerLogin", root, HelpBinds)

Client

local screenW, screenH = guiGetScreenSize()
function ShowHelp()
		guiSetInputEnabled(false)
		showCursor(true)


        helpWindow = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
        guiWindowSetSizable(helpWindow, false)
        guiSetAlpha(helpWindow, 0.92)
        guiSetProperty(helpWindow, "CaptionColour", "FFD7E21C")

        CloseInfo = guiCreateLabel(230, 499, 196, 35, "F1 to close window", false, helpWindow)
        guiSetProperty(CloseInfo, "NormalTextColour", "FFFE0000")
        helphelpTabsPan = guiCreateTabPanel(9, 28, 572, 461, false, helpWindow)

        helpTabs = guiCreateTab("Tab1", helphelpTabsPan)

        memo = guiCreateMemo(6, 32, 561, 399, "Line 1. \n\nLine 2.", false, helpTabs)
        label = guiCreateLabel(13, 10, 544, 26, "Label", false, helpTabs)
        guiLabelSetColor(label, 255, 100, 25)

        helpTabs2 = guiCreateTab("Tab2", helphelpTabsPan)

        memo2 = guiCreateMemo(5, 32, 561, 399, "Line 1 \n/Line 2", false, helpTabs2)
        label2 = guiCreateLabel(15, 10, 544, 26, "Label", false, helpTabs2)
        guiLabelSetColor(label2, 255, 100, 25)

        helpTabs3 = guiCreateTab("Tab3", helphelpTabsPan)

        memo3 = guiCreateMemo(5, 32, 561, 399, "Line 1.\nLine2.", false, helpTabs3)  		
end
addEvent("Help_Login", true)
addEventHandler("Help_Login", getRootElement(), ShowHelp)

function DestroyGUI() 
	if ( guiGetVisible ( helpWindow ) == false ) then            
		guiSetVisible ( helpWindow, true ) 
		showCursor(true)
	else              
		guiSetVisible ( helpWindow, false ) 
		showCursor(false)
	end
end        




function binds()
	bindKey("f1", "down", DestroyGUI)
end
addEvent("Help_Panel", true)
addEventHandler("Help_Panel", getRootElement(), binds)

This works perfect, no problems in the code.  But if you test this and remove the event "Help_Login" you will see my issue...  It doesn't think a GUI is there, how can you keep a GUI closed for user but still bind it's window to a key? :P 

Link to comment

So you're basically telling the bindKey is not made anymore so.

addEvent( "Help_Login", true )

local guiElements = {}

local screenW, screenH = guiGetScreenSize()

function switchGUI() 
	if ( guiElements[ 1 ] ) then
    	showCursor( false )
		
		for i = 1, #guiElements do
			local theElement = guiElements[ i ]
			destroyElement( theElement )
		end
    else
    	guiSetInputEnabled(false)
		showCursor(true)


        guiElements[ 1 ] = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
        guiWindowSetSizable(guiElements[ 1 ], false)
        guiSetAlpha(guiElements[ 1 ], 0.92)
        guiSetProperty(guiElements[ 1 ], "CaptionColour", "FFD7E21C")

        guiElements[ 2 ] = guiCreateLabel(230, 499, 196, 35, "F1 to close window", false, guiElements[ 1 ])
        guiSetProperty(guiElements[ 2 ], "NormalTextColour", "FFFE0000")
        guiElements[ 3 ] = guiCreateTabPanel(9, 28, 572, 461, false, guiElements[ 1 ])

        guiElements[ 4 ] = guiCreateTab("Tab1", guiElements[ 3 ])

        guiElements[ 5 ] = guiCreateMemo(6, 32, 561, 399, "Line 1. \n\nLine 2.", false, guiElements[ 4 ] )
        guiElements[ 6 ] = guiCreateLabel(13, 10, 544, 26, "Label", false, guiElements[ 4 ] )
        guiLabelSetColor( guiElements[ 6 ], 255, 100, 25)

        guiElements[ 7 ] = guiCreateTab("Tab2", guiElements[ 3 ])

        guiElements[ 8 ] = guiCreateMemo(5, 32, 561, 399, "Line 1 \n/Line 2", false, guiElements[ 7 ])
        guiElements[ 9 ] = guiCreateLabel(15, 10, 544, 26, "Label", false, guiElements[ 7 ])
        guiLabelSetColor( guiElements[ 9 ], 255, 100, 25)

        guiElements[ 10 ] = guiCreateTab("Tab3", guiElements[ 3 ])

        guiElements[ 11 ] = guiCreateMemo(5, 32, 561, 399, "Line 1.\nLine2.", false, guiElements[ 10 ] )  	
    end
end        

bindKey( "f1", "down", switchGUI )

addEventHandler( "Help_Login", getRootElement(), switchGUI )

 

Edited by Uknown.
Link to comment

:/ That permanently destroys the GUI when I press F1...

WARNING:GUI\help.lua:10:Bad argument @ 'destroyElement' [Expected element at argument 1]

That's my debugscript 3, I am not really experienced with stuff like 

		for i = 1, #guiElements do
			local theElement = guiElements[ i ]
			destroyElement( theElement )

All I know is it basically groups it...

Link to comment

So you don't want to destroy it then no problem.

 

    addEvent( "Help_Login", true )
    local guiElements = {}
    local screenW, screenH = guiGetScreenSize()
    function switchGUI() 
    	if ( guiElements[ 1 ] ) then
    		if ( guiGetVisible( guiElements[ 1 ] ) then    	
    			showCursor( false )
    		
                guiSetVisible( guiElements[ 1 ], false )
        	else
        		guiSetInputEnabled(false)
    			showCursor(true)
        
        		guiSetVisible( guiElements[ 1 ], true )
        	end
        else
        	guiSetInputEnabled(false)
    		showCursor(true)
            guiElements[ 1 ] = guiCreateWindow((screenW - 591) / 2, (screenH - 534) / 2, 591, 534, "Information", false)
            guiWindowSetSizable(guiElements[ 1 ], false)
            guiSetAlpha(guiElements[ 1 ], 0.92)
            guiSetProperty(guiElements[ 1 ], "CaptionColour", "FFD7E21C")
            guiElements[ 2 ] = guiCreateLabel(230, 499, 196, 35, "F1 to close window", false, guiElements[ 1 ])
            guiSetProperty(guiElements[ 2 ], "NormalTextColour", "FFFE0000")
            guiElements[ 3 ] = guiCreateTabPanel(9, 28, 572, 461, false, guiElements[ 1 ])
            guiElements[ 4 ] = guiCreateTab("Tab1", guiElements[ 3 ])
            guiElements[ 5 ] = guiCreateMemo(6, 32, 561, 399, "Line 1. \n\nLine 2.", false, guiElements[ 4 ] )
            guiElements[ 6 ] = guiCreateLabel(13, 10, 544, 26, "Label", false, guiElements[ 4 ] )
            guiLabelSetColor( guiElements[ 6 ], 255, 100, 25)
            guiElements[ 7 ] = guiCreateTab("Tab2", guiElements[ 3 ])
            guiElements[ 8 ] = guiCreateMemo(5, 32, 561, 399, "Line 1 \n/Line 2", false, guiElements[ 7 ])
            guiElements[ 9 ] = guiCreateLabel(15, 10, 544, 26, "Label", false, guiElements[ 7 ])
            guiLabelSetColor( guiElements[ 9 ], 255, 100, 25)
            guiElements[ 10 ] = guiCreateTab("Tab3", guiElements[ 3 ])
            guiElements[ 11 ] = guiCreateMemo(5, 32, 561, 399, "Line 1.\nLine2.", false, guiElements[ 10 ] )  	
        end
    end        
    bindKey( "f1", "down", switchGUI )
    addEventHandler( "Help_Login", getRootElement(), switchGUI )

 

Link to comment

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...