View Single Post
Old 06-30-2019, 08:20 PM   #510
Human being with feelings
Lokasenna's Avatar
Join Date: Sep 2008
Location: Calgary, AB, Canada
Posts: 6,257

Originally Posted by woodslanding View Post
I'm trying to create a matrix of buttons, and I'm trying to get each one to call a master method (called 'select') with its own index as an argument:

for i = 1,buttonCount

    --create button, etc

    ctl.onmouseup = function(self) select(i) GUI.IButton.onmouseup(self) end
and it doesn't seem to work. I feel like I don't quite understand what happens when you create this kind of function... does the value of 'i' not get stored with the function?
I would actually expect it to store i there, since you're creating the function within i's scope. Try adding a GUI.Msg(i) in the function as well to see if it's being called and if it has i.

Regardless, there is a better way to do what you want - Buttons already have a hook for running a user function, and it specifically does take extra parameters.

ctl.func = select -- note: no ()s here
ctl.params = {i}
Everything in params is unpacked and passed as arguments to func.
Lokasenna is offline   Reply With Quote