Jump to content

button


Recommended Posts

How to press a button with using a command?

I mean, not an ordinary click on the gui (onClientGUIClick) when the player presses the button.

I mean how to do with 

addCommandHandler

to make the button click

 

Example:
 

addEventHandler("onClientGUIClick", root, function()
	if source == button1 then
	outputChatBox ("HI", 231,217,176,false )
	end
end)

function zxcs()
-- text for using button 
end
addEvent("zxcs", true)
addEventHandler( "zxcs", localPlayer, zxcs )
addCommandHandler ( "z", zxcs )

I want when the player writes in the chat /z 

the button must be pressed

Link to comment
local myButton = -- your button code
addEventHandler("onClientGUIClick", myButton, myFunction, false) -- this event call the function when the button is clicked

function myFunction()
  -- what to do when the button is clicked
end

addCommandHandler ( "z", myFunction )

 

Edited by Spakye
  • Like 1
Link to comment
14 minutes ago, Spakye said:

local myButton = -- your button code
addEventHandler("onClientGUIClick", myButton, myFunction, false) -- this event call the function when the button is clicked

function myFunction()
  -- what to do when the button is clicked
end

addCommandHandler ( "z", myFunction )

 

local mybutton = guiCreateButton(1000, 200, 75, 30, "ACC", false) 

addEventHandler("onClientGUIClick",mybutton,myFunction,false)
function myFunction()
outputChatBox ("test", 231,217,176,false )
end
addCommandHandler("z",myFunction)

I misunderstood something?

Using the command /z I get the text in the chat (test)

but when I press the button. Nothing happens

I just wrote a button click differently in the code(1 post in this topic), and now I don't know how to write it correctly.

Link to comment
2 minutes ago, Spakye said:

I tried the code and i have an error, the problem is the function comes after the handler, place the function before.

OK, thank you. Everything works with your code.

But I'm trying to use my button, which is in another window, and I can't get it to work.

function window()
		window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false)
        windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window)
end

function myFunction()
outputChatBox ("test", 231,217,176,false )
end
addCommandHandler("z",myFunction)
addEventHandler("onClientGUIClick",windowbutton1,myFunction,false)

I press the button and nothing happens

Link to comment
5 minutes ago, Spakye said:

Do you call the function window() at some point?

Yes of course. How can I open a window differently if I don't call it

Maybe I misunderstand something, but I do it like this:

function window()
		window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false)
        windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window)
end

function start()
window()
end
addCommandHandler ( "w", start )

function myFunction()
outputChatBox ("test", 231,217,176,false )
end
addCommandHandler("z",myFunction)
addEventHandler("onClientGUIClick",windowbutton1,myFunction,false)

 

Link to comment

Ok so the problem is the event handler for the button is out of the function, wich means when the ressource start it can't find the button as it was not created yet so it fails.

I recommend you to do something like this :

function createGuis()
  --in this function you place all the guis and event handler attached to guis element
  window = guiCreateWindow((sW - 406) / 2, (sH - 461) / 2, 455, 465, "Window", false)
  windowbutton1 = guiCreateButton(330, 432, 118, 22, "butt", false, window)
  addEventHandler("onClientGUIClick",windowbutton1,myFunction,false)
  guiSetVisible(window, false) -- we hide the window for now, so you can decide when its gonna be visible
end

function showWindow()
	guiSetvisible(window, true)  
end
addCommandHandler ( "w", showWindow )

function myFunction()
	outputChatBox ("test", 231,217,176,false )
end
addCommandHandler("z",myFunction)

function ressourceStarted()
	createGuis()
end
addEventHandler("onClientResourceStart", getResourceRootElement(getThisResource()), resourceStarted) -- this event is triggered when the resource start client side

The guis elements are created when the resource start and we hide them, then you show them when needed with showWindow()

Edited by Spakye
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...