Help: Dynamic newButton

Hello guys,

Do you guys know how to set up a Dynamic newButton? I’ve tried, but I’m stuck on the “onEvent” of the button parameters my code goes like this:

local t= totalActiveFile --this is the dynamic part where in a query gives its value local topPos = 80 for i = 1 , t do local slotName = ("saveSlot" .. i) slotName = widget.newButton { left = 50, top =topPos , width = 230, height = 100, defaultFile = "images/loadslot.png", overFile = "images/loadslotpress.png", id = ("load\_slot" .. i), onEvent = loadGame .. i, } button:insert(slotName) topPos = topPos + 100 end

If you have a suggestion or idea, please let me know.

Thank you for your time and patience,

Jam

onEvent = loadGame … i,

What you pass to onEvent must be a function or more accurately a pointer to that function.  What you are passing to onEvent here is whatever value is in the variable loadGame + the value of the variable i.  Since I suspect loadGame isn’t a real variable, it’s probably nil, so you’re passing just the value of i to this which isn’t a function.

You probably should setup a table called “loadGameHandlers” and to each cell in the table each function that you want, perhaps something like:

local loadGameHandlers = {}

loadGameHandlers[1] = function(event)

      – code for handler 1

end

loadGameHandlers[2] = function(event)

      – code for handler 3

end

loadGameHandlers[3] = function(event)

      – code for handler 3

end

then you can do this for your onEvent code:

onEvent = loadGameHandlers[i]

Thank Rob! it works like a charm :slight_smile:

onEvent = loadGame … i,

What you pass to onEvent must be a function or more accurately a pointer to that function.  What you are passing to onEvent here is whatever value is in the variable loadGame + the value of the variable i.  Since I suspect loadGame isn’t a real variable, it’s probably nil, so you’re passing just the value of i to this which isn’t a function.

You probably should setup a table called “loadGameHandlers” and to each cell in the table each function that you want, perhaps something like:

local loadGameHandlers = {}

loadGameHandlers[1] = function(event)

      – code for handler 1

end

loadGameHandlers[2] = function(event)

      – code for handler 3

end

loadGameHandlers[3] = function(event)

      – code for handler 3

end

then you can do this for your onEvent code:

onEvent = loadGameHandlers[i]

Thank Rob! it works like a charm :slight_smile: