|
| 24 Mar 2016 10:05 PM |
I'm trying to pass extra parameters to an event listener.
-- I'm trying to convert this to a non-anonymous function imgButton.MouseButton1Down:connect(function(button) button = imgButton print(button.Name) end)
But, if I try, then how do I pass the button parameter to the :connect() method?
for example: function buttonClicked(button) print(button.Name) end
imgButton.MouseButton1Down:connect(buttonClicked) -- ** I don't know how to pass the imgButton as an argument to the function |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:25 PM |
function buttonClicked(button, imgButton) print(button.Name) end
#code print("lol im batman") |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:38 PM |
@Expensive that code didn't do anything, it just added another parameter to the function. He wanted to know how to pass the parameter from the event he connected the function to.
To answer your question, I believe you would pass it as normal:
function buttonClicked(button) print(button.Name) end
imgButton.MouseButton1Down:connect(buttonClicked(imgButton))
|
|
|
| Report Abuse |
|
|
Casualist
|
  |
| Joined: 26 Jun 2014 |
| Total Posts: 4443 |
|
|
| 24 Mar 2016 10:44 PM |
@2kool function buttonClicked(button) print(button.Name) end
imgButton.MouseButton1Down:connect(buttonClicked(imgButton))
effectively behaves like:
function buttonClicked(button) print(button.Name) end
buttonClicked(imgButton) imgButton.MouseButton1Down:connect(nil)
@OP This is what you want:
function buttonClicked(button) print(button.Name) end
imgButton.MouseButton1Down:connect(function()buttonClicked(imgButton)end)
|
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 10:52 PM |
| Thanks. I always use lambda functions when I connect events. |
|
|
| Report Abuse |
|
|
TimeTicks
|
  |
| Joined: 27 Apr 2011 |
| Total Posts: 27115 |
|
|
| 24 Mar 2016 11:28 PM |
save lines of code and just put the function inside the connection
thing.MouseButton1Click:connect(function() --all your stuff in here end)
looks cleaner imo |
|
|
| Report Abuse |
|
|
|
| 24 Mar 2016 11:33 PM |
| @TimeTicks Anonymous functions are great, but sometimes you want to be able to call the function from other points in the script. Also his question was how not to use anonymous functions. |
|
|
| Report Abuse |
|
|