Guest Posted November 18, 2008 Share Posted November 18, 2008 Hey guys, I have this code: addEventHandler("onClientGUIClick", myButton, testFunction, false) function spawn(message) outputChatBox(message) end So of course nothing happens, because message is nil, but if I change the first line to: addEventHandler("onClientGUIClick", myButton, testFunction("This is a test!"), false) I would naturally expect for it to output "This is a test!" when I press my button but instead it behaves weirdly, outputting the text right when its loaded and doing nothing when I press the button. What am I doing wrong? Thanks. Link to comment
darkdreamingdan Posted November 19, 2008 Share Posted November 19, 2008 You dont pass parameters in addEventHandler, you would have to store your text externally and it would have to look like this: --A table that stores messages according to the button local messages = { [myButton] = "You clicked the 'myButton' button!" } function spawn(message) outputChatBox(message) end addEventHandler("onClientGUIClick", myButton, spawn, false) Note that you'd have to create the table after all the buttons are created. Just to be clear, the parameters that are passed into a handler function are those listed on the wiki for that specific event, and cannot be changed (unlike bindKey for example). Link to comment
arc_ Posted November 19, 2008 Share Posted November 19, 2008 (Xeon06: first of all I'll be assuming that the "spawn" function in your code was meant to be named "testFunction", or vice versa) addEventHandler("onClientGUIClick", myButton, testFunction("This is a test!"), false) I would naturally expect for it to output "This is a test!" when I press my button but instead it behaves weirdly, outputting the text right when its loaded and doing nothing when I press the button. Like you already noticed, this is not the way it works. What you are doing in that code is calling testFunction right there, on that addEventHandler line; and then using its return value as the handler argument to addEventHandler. Obviously testFunction doesn't return anything, so you end up doing the equivalent of this: testFunction("This is a test!") addEventHandler("onClientGUIClick", myButton, nil, false) To make it work like you want it to, you would have to write code like this: addEventHandler("onClientGUIClick", myButton, function() testFunction("This is a test!") end, false) As you see, now you are passing an (anonymous) function to addEventHandler. Once the event is triggered, this function is called, which will in turn call testFunction with any arguments you want. Seeing as addEventHandler doesn't let you specify handler arguments like bindKey does, you have to take detours like this I'd also like to make a correction to Talidan's code, as it is not entirely correct (sorry Talidan): --A table that stores messages according to the button local messages = { [myButton] = "You clicked the 'myButton' button!" } function spawn() outputChatBox(messages[source]) -- grab the message from the "messages" table using the source element (the one that was clicked) as key end addEventHandler("onClientGUIClick", myButton, spawn, false) 1 Link to comment
Guest Posted November 21, 2008 Share Posted November 21, 2008 Ah, thank you very much to both of you. Link to comment
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now