Looking for a shorter way to write a function

I have a function that I want  to write shorter.  Perhaps with a for loop?  It will activate 80 buttons.

[lua]

function adTouch()

    myButton1:setEnabled( true )

    myButton2:setEnabled( true )

    myButton3:setEnabled( true )

    myButton4:setEnabled( true )

    myButton5:setEnabled( true )

    myButton6:setEnabled( true )

    myButton7:setEnabled( true )

    myButton8:setEnabled( true )

    myButton9:setEnabled( true )

    myButton10:setEnabled( true )

    myButton11:setEnabled( true )

    myButton12:setEnabled( true )

    myButton13:setEnabled( true )

    myButton14:setEnabled( true )

    myButton15:setEnabled( true )

    myButton16:setEnabled( true )

    myButton17:setEnabled( true )

    myButton18:setEnabled( true )

    myButton19:setEnabled( true )

    myButton20:setEnabled( true )

    myButton21:setEnabled( true )

    myButton22:setEnabled( true )

    myButton23:setEnabled( true )

    myButton24:setEnabled( true )

    myButton25:setEnabled( true )

    myButton26:setEnabled( true )

    myButton27:setEnabled( true )

    myButton28:setEnabled( true )

    myButton29:setEnabled( true )

    myButton30:setEnabled( true )

    myButton31:setEnabled( true )

    myButton32:setEnabled( true )

    myButton33:setEnabled( true )

    myButton34:setEnabled( true )

    myButton35:setEnabled( true )

    myButton36:setEnabled( true )

    myButton37:setEnabled( true )

    myButton38:setEnabled( true )

    myButton39:setEnabled( true )

    myButton40:setEnabled( true )

    myButton41:setEnabled( true )

    myButton42:setEnabled( true )

    myButton43:setEnabled( true )

    myButton44:setEnabled( true )

    myButton45:setEnabled( true )

    myButton46:setEnabled( true )

    myButton47:setEnabled( true )

    myButton48:setEnabled( true )

    myButton49:setEnabled( true )

    myButton50:setEnabled( true )

    myButton51:setEnabled( true )

    myButton52:setEnabled( true )

    myButton53:setEnabled( true )

    myButton54:setEnabled( true )

    myButton55:setEnabled( true )

    myButton56:setEnabled( true )

    myButton57:setEnabled( true )

    myButton58:setEnabled( true )

    myButton59:setEnabled( true )

    myButton60:setEnabled( true )

    myButton61:setEnabled( true )

    myButton62:setEnabled( true )

    myButton63:setEnabled( true )

    myButton64:setEnabled( true )

    myButton65:setEnabled( true )

    myButton66:setEnabled( true )

    myButton67:setEnabled( true )

    myButton68:setEnabled( true )

    myButton69:setEnabled( true )

    myButton70:setEnabled( true )

    myButton71:setEnabled( true )

    myButton72:setEnabled( true )

    myButton73:setEnabled( true )

    myButton74:setEnabled( true )

    myButton75:setEnabled( true )

    myButton76:setEnabled( true )

    myButton77:setEnabled( true )

    myButton78:setEnabled( true )

    myButton79:setEnabled( true )

    myButton80:setEnabled( true )

end

[/lua]

the two topics you should learn are arrays and loops. that is, you could create an array of 80 buttons (instead of 80 individual variables) and use a loop to access them.

-- creation: var buttonList = {} for i=1,80 do buttonList[i] = widget.newButton( ... or whatever you do to create your buttons end -- access: for i=1,80 do buttonList[i]:setEnabled(true) end

Having said that, (and recommending it) you can also “cheat” a solution with your existing buttons, since Lua variables are all just table elements *somewhere*:

-- set this to whatever "owns" your current variables -- a scene for example maybe? or \_G if global local theOwner = \_G for i = 1,80 do theOwner["myButton"..i]:setEnabled(true) end

Thank you sir, yes have been studying these

https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

still having a little trouble wrapping my head around the whole thing.  Now I know I have to create the  Array first then loop it!

Interesting the second method worked perfectly.

When using the first method I get error:

Error loading module ‘start’ from file ‘/Users/russm305/Desktop/KENO/start.lua’:

    /Users/russm305/Desktop/KENO/start.lua:92: ‘=’ expected near ‘buttonList’

so this line …var buttonList = {}    is drawing an error somehow, any ideas?

I trie this also:

buttonList = {}

for i=1,80 do

  buttonList[i] = myButton

end

– access:

for i=1,80 do

  buttonList[i]:setEnabled(true)

end

But if I click the number twice to myButton:setEnabled(false)  I get error…

attempt to index field ‘?’ (a nil value)

 

 

 

Question:   Since the second method is working very well I am going with that, is their any bugs or problems associated with going that route?

Are these correct way of using it multiple times?

    local theOwnerF = _G

for i = 1,80 do

  theOwnerF[“flag”…i]=false

end

    local theOwner1 = _G

for i = 1,80 do

  theOwner1[“myButton”…i]:setEnabled(false)

end

    local theOwner = _G

for i = 1,80 do

  theOwner[“myButton”…i]:setEnabled(true)

end

That should be:

local buttonList = {}

(looks like somebody was in “C# mode”  :P)

:smiley: yep, doh, oops, typo, javascript, s/b “local” as per ingemar

I trie this also:

 

buttonList = {}

for i=1,80 do

  buttonList[i] = myButton

end

 

– access:

for i=1,80 do

  buttonList[i]:setEnabled(true)

end

 

But if I click the number twice to myButton:setEnabled(false)  I get error…

attempt to index field ‘?’ (a nil value)

 

You’re creating 80 references to the same button there (not sure where you’re getting myButton from either)

Also once you have the buttons in a table you should only be referring to buttonList[i]:setEnabled().

The reason you’re getting an error is because myButton has fallen out of scope.

Thank you, i edited my response above in reference to the second method.  is that an acceptable method

Question:   Since the second method is working very well I am going with that, is their any bugs or problems associated with going that route?

 

Are these correct way of using it multiple times?

 

    local theOwnerF = _G

for i = 1,80 do

  theOwnerF[“flag”…i]=false

end

 

    local theOwner1 = _G

for i = 1,80 do

  theOwner1[“myButton”…i]:setEnabled(false)

end

 

    local theOwner = _G

for i = 1,80 do

  theOwner[“myButton”…i]:setEnabled(true)

end

the two topics you should learn are arrays and loops. that is, you could create an array of 80 buttons (instead of 80 individual variables) and use a loop to access them.

-- creation: var buttonList = {} for i=1,80 do buttonList[i] = widget.newButton( ... or whatever you do to create your buttons end -- access: for i=1,80 do buttonList[i]:setEnabled(true) end

Having said that, (and recommending it) you can also “cheat” a solution with your existing buttons, since Lua variables are all just table elements *somewhere*:

-- set this to whatever "owns" your current variables -- a scene for example maybe? or \_G if global local theOwner = \_G for i = 1,80 do theOwner["myButton"..i]:setEnabled(true) end

Thank you sir, yes have been studying these

https://coronalabs.com/blog/2011/06/21/understanding-lua-tables-in-corona-sdk/

still having a little trouble wrapping my head around the whole thing.  Now I know I have to create the  Array first then loop it!

Interesting the second method worked perfectly.

When using the first method I get error:

Error loading module ‘start’ from file ‘/Users/russm305/Desktop/KENO/start.lua’:

    /Users/russm305/Desktop/KENO/start.lua:92: ‘=’ expected near ‘buttonList’

so this line …var buttonList = {}    is drawing an error somehow, any ideas?

I trie this also:

buttonList = {}

for i=1,80 do

  buttonList[i] = myButton

end

– access:

for i=1,80 do

  buttonList[i]:setEnabled(true)

end

But if I click the number twice to myButton:setEnabled(false)  I get error…

attempt to index field ‘?’ (a nil value)

 

 

 

Question:   Since the second method is working very well I am going with that, is their any bugs or problems associated with going that route?

Are these correct way of using it multiple times?

    local theOwnerF = _G

for i = 1,80 do

  theOwnerF[“flag”…i]=false

end

    local theOwner1 = _G

for i = 1,80 do

  theOwner1[“myButton”…i]:setEnabled(false)

end

    local theOwner = _G

for i = 1,80 do

  theOwner[“myButton”…i]:setEnabled(true)

end

That should be:

local buttonList = {}

(looks like somebody was in “C# mode”  :P)

:smiley: yep, doh, oops, typo, javascript, s/b “local” as per ingemar

I trie this also:

 

buttonList = {}

for i=1,80 do

  buttonList[i] = myButton

end

 

– access:

for i=1,80 do

  buttonList[i]:setEnabled(true)

end

 

But if I click the number twice to myButton:setEnabled(false)  I get error…

attempt to index field ‘?’ (a nil value)

 

You’re creating 80 references to the same button there (not sure where you’re getting myButton from either)

Also once you have the buttons in a table you should only be referring to buttonList[i]:setEnabled().

The reason you’re getting an error is because myButton has fallen out of scope.

Thank you, i edited my response above in reference to the second method.  is that an acceptable method

Question:   Since the second method is working very well I am going with that, is their any bugs or problems associated with going that route?

 

Are these correct way of using it multiple times?

 

    local theOwnerF = _G

for i = 1,80 do

  theOwnerF[“flag”…i]=false

end

 

    local theOwner1 = _G

for i = 1,80 do

  theOwner1[“myButton”…i]:setEnabled(false)

end

 

    local theOwner = _G

for i = 1,80 do

  theOwner[“myButton”…i]:setEnabled(true)

end