Setting 80 flags to 'true'

Im making a game that will have 80 widgets with flags that can be set to true/false.  At the beginning of the code instead of doing this…

local flag1 = true

local flag2 = true

local flag3 = true

local flag4 = true

local flag5 = true…

I want to do something more like this…

for i = 1, 80 do

   flag(i) = true

end

But I can’t get it to work properly, can someone please point out what I’m doing incorrectly.  Thank you!

You want to use brackets and not parenthesis’

for i = 1, 80 do flag[i] = true end

Try that out.

Thank you, okay I did that and got an error  ‘attempt to index global ‘flag’ (a nil value)’

so I tried…

for i = 1, 80 do

   local flag[i] = true

end

got error… unexpected symbol near ‘[’

Also tried…

local flag

for i = 1, 80 do

   flag[i] = true

end

this error…attempt to index local ‘flag’ (a nil value)

What else can I be doing wrong that is causing these errors?  

Sorry m’friend, I thought you had the beginning portion worked out. Try this:

local flag = {} for i = 1, 80 do flag[i] = true print("flag"..i.." = "..tostring(flag[i])) end

I’d suggest reading up on tables in Corona. This blogpost was invaluable when I was learning at first:

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

Awesome thank you!

Would it be possible to do something like this:  I want to write one function not many.

[lua]local onEvent = {}

for v = 1,3 do

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/[v].png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end    

  myButton1 = widget.newButton{

        defaultFile = “NumbersA/1.png”,

        left = w/2,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent1

    }

    screenGroup:insert(myButton1)

 myButton2 = widget.newButton{

        defaultFile = “NumbersA/2.png”,

        left = w/2+70,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent2

    }

    screenGroup:insert(myButton2)

myButton3 = widget.newButton{

        defaultFile = “NumbersA/3.png”,

        left = w/2+140,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent3

    }

    screenGroup:insert(myButton3)

[/lua]

So, a couple things here:

  1. You’re probably going want to change the name of your onEvent table, as that is already the name of a function. Try using something like “touchWidget” or similar.

  2. I don’t think your ‘NumbersB/[v].png’ isn’t going to be interrepted the way you want. you might need to generate a separate function to identify which image you want to show.

  3. Your print statements aren’t going to show anything but the text “flag[v] off”. Try using print(“flag[”…v…"] = "…tostring(flag[v]))

  4. Whichever name you give your functions, you will want to use the brackets around them when declaring them as listeners. Something like touchWidget[1] instead of touchWidget1.

Other than that, I think you’re on the right rack.

    1.   Im confused inserted  touchWidget as table…now it can’t index onEvent because its Global, do I change the name of onEvent function?  

[lua]

local touchWidget = {}

for v = 1,3 do

    --print(“onEvent”…v…" = "…tostring(onEvent[v]))

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/[v].png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end    [/lua]

  1. Thank you, I have a game with 80 buttons(widgets) #1-80  with events and flags attached.  So I was trying to step my game up and use for loops instead of 80 separate widgets with events.  But if it doesn’t allow me to generate the different #  image for each button it wont’t work.  How would you do it?  A for loop or hard code it or a different way?

[lua]

 B[v] = display.newImageRect(“NumbersB/”…v…".png",50,50)

[/lua]

Thanks Nick Sherman that is awesome, can you tell me what is wrong here, Im keep getting an error.  Im eventually going  to attempt to make 80 widgets with a for loop.  Thank you!

[lua]local touchWidget = {}

for v = 1,3 do

    --print(“onEvent”…v…" = "…tostring(onEvent[v]))

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/"…v…".png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end   

     myButton1 = widget.newButton{

        defaultFile = “NumbersA/1.png”,

        left = w/2,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent1

    }

    screenGroup:insert(myButton1)

     myButton2 = widget.newButton{

        defaultFile = “NumbersA/2.png”,

        left = w/2+70,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent2

    }

    screenGroup:insert(myButton2)

     myButton3 = widget.newButton{

        defaultFile = “NumbersA/3.png”,

        left = w/2+140,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent3

    }

    screenGroup:insert(myButton3)

[/lua]

You want to use brackets and not parenthesis’

for i = 1, 80 do flag[i] = true end

Try that out.

Thank you, okay I did that and got an error  ‘attempt to index global ‘flag’ (a nil value)’

so I tried…

for i = 1, 80 do

   local flag[i] = true

end

got error… unexpected symbol near ‘[’

Also tried…

local flag

for i = 1, 80 do

   flag[i] = true

end

this error…attempt to index local ‘flag’ (a nil value)

What else can I be doing wrong that is causing these errors?  

Sorry m’friend, I thought you had the beginning portion worked out. Try this:

local flag = {} for i = 1, 80 do flag[i] = true print("flag"..i.." = "..tostring(flag[i])) end

I’d suggest reading up on tables in Corona. This blogpost was invaluable when I was learning at first:

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

Awesome thank you!

Would it be possible to do something like this:  I want to write one function not many.

[lua]local onEvent = {}

for v = 1,3 do

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/[v].png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end    

  myButton1 = widget.newButton{

        defaultFile = “NumbersA/1.png”,

        left = w/2,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent1

    }

    screenGroup:insert(myButton1)

 myButton2 = widget.newButton{

        defaultFile = “NumbersA/2.png”,

        left = w/2+70,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent2

    }

    screenGroup:insert(myButton2)

myButton3 = widget.newButton{

        defaultFile = “NumbersA/3.png”,

        left = w/2+140,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent3

    }

    screenGroup:insert(myButton3)

[/lua]

So, a couple things here:

  1. You’re probably going want to change the name of your onEvent table, as that is already the name of a function. Try using something like “touchWidget” or similar.

  2. I don’t think your ‘NumbersB/[v].png’ isn’t going to be interrepted the way you want. you might need to generate a separate function to identify which image you want to show.

  3. Your print statements aren’t going to show anything but the text “flag[v] off”. Try using print(“flag[”…v…"] = "…tostring(flag[v]))

  4. Whichever name you give your functions, you will want to use the brackets around them when declaring them as listeners. Something like touchWidget[1] instead of touchWidget1.

Other than that, I think you’re on the right rack.

    1.   Im confused inserted  touchWidget as table…now it can’t index onEvent because its Global, do I change the name of onEvent function?  

[lua]

local touchWidget = {}

for v = 1,3 do

    --print(“onEvent”…v…" = "…tostring(onEvent[v]))

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/[v].png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end    [/lua]

  1. Thank you, I have a game with 80 buttons(widgets) #1-80  with events and flags attached.  So I was trying to step my game up and use for loops instead of 80 separate widgets with events.  But if it doesn’t allow me to generate the different #  image for each button it wont’t work.  How would you do it?  A for loop or hard code it or a different way?

[lua]

 B[v] = display.newImageRect(“NumbersB/”…v…".png",50,50)

[/lua]

Thanks Nick Sherman that is awesome, can you tell me what is wrong here, Im keep getting an error.  Im eventually going  to attempt to make 80 widgets with a for loop.  Thank you!

[lua]local touchWidget = {}

for v = 1,3 do

    --print(“onEvent”…v…" = "…tostring(onEvent[v]))

    onEvent[v] = function (event)

        if event.phase == “ended” then

            --audio.play(s5)

            if flag[v] == true then

                B[v] = display.newImageRect(‘NumbersB/"…v…".png’,50,50)

                B[v].x = myButton[v].x

                B[v].y = myButton[v].y

                screenGroup:insert(B[v])

                print (‘flag[v] on’)

                flag[v]=false

            elseif flag[v]==false then

                display.remove( B[v] )

                B[v]=nil

                 flag[v]=true

                 print (‘flag[v] off’)

             end

            

        end

    end

end   

     myButton1 = widget.newButton{

        defaultFile = “NumbersA/1.png”,

        left = w/2,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent1

    }

    screenGroup:insert(myButton1)

     myButton2 = widget.newButton{

        defaultFile = “NumbersA/2.png”,

        left = w/2+70,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent2

    }

    screenGroup:insert(myButton2)

     myButton3 = widget.newButton{

        defaultFile = “NumbersA/3.png”,

        left = w/2+140,

        top = h/2,

          width =w1, height = h1,

         onEvent = onEvent3

    }

    screenGroup:insert(myButton3)

[/lua]