data module

Hello. I have a table in file “Quest1”. How to insert variables into a group in the main scene?

this is my table

 local vpr = {}
 function vpr:vop()

     vpr.tekst1 = display.newText( "8", display.contentCenterX - 10 , display.contentCenterY - 170, "MyriadPro-Regular.otf", 28 )
     vpr.tekst1:setFillColor(230/255,230/255,230/255,1)


     vpr.tekst2 = display.newText( "E", display.contentCenterX + 10 , display.contentCenterY - 170, "WebMobile-Regular.ttf", 56 )
     vpr.tekst2:setFillColor(132/255,0/255,50/255,1)

     vpr.tekst3 = display.newText( "-", display.contentCenterX - 30 , display.contentCenterY - 155, "MyriadPro-Regular.otf", 26 )
     vpr.tekst3:setFillColor(230/255,230/255,230/255,1)

     vpr.tekst4 = display.newText( "2", display.contentCenterX - 10 , display.contentCenterY - 140, "MyriadPro-Regular.otf", 28 )
     vpr.tekst4:setFillColor(230/255,230/255,230/255,1)

     vpr.tekst5 = display.newText( "2", display.contentCenterX + 10 , display.contentCenterY - 140, "MyriadPro-Regular.otf", 28 )
     vpr.tekst5:setFillColor(230/255,230/255,230/255,1)

     vpr.tekst6 = display.newText( "6", display.contentCenterX - 10 , display.contentCenterY - 100, "MyriadPro-Regular.otf", 28 )
     vpr.tekst6:setFillColor(230/255,230/255,230/255,1)

     vpr.tekst7 = display.newText( "3", display.contentCenterX + 10 , display.contentCenterY - 100, "MyriadPro-Regular.otf", 28 )
     vpr.tekst7:setFillColor(230/255,230/255,230/255,1)

     vpr.tekst8 = display.newText( "E", display.contentCenterX - 20 , display.contentCenterY - 20, "WebMobile-Regular.ttf", 56 )
     vpr.tekst8:setFillColor(132/255,0/255,50/255,1)

    vpr.tekst9 = display.newText( "= ?", display.contentCenterX + 20  , display.contentCenterY - 20, "MyriadPro-Regular.otf", 28 )
    vpr.tekst9:setFillColor(230/255,230/255,230/255,1)

    local vopros = display.newRoundedRect( display.contentCenterX , display.contentCenterY - 120  , 50, 1, 0 )
        vopros.strokeWidth = 1/2
        vopros:setFillColor( 230/255,230/255,230/255,1 )
        vopros:setStrokeColor( 230/255,230/255,230/255,1 )

    end

     vpr.otvet = "5"

  return vpr

how to loop through all table values ​​and insert into sceneGroup

   local vpr = require("Questions.Quest" .. settings.TL )
   local sceneGroup = self.view
   vpr:vop()
   for i = 1 , #vpr do
        sceneGroup:insert(vpr[i])
        i = i + 1
   end

There are a number of problems here.

Firstly, you do not need to perform the line
i = i + 1
The loop will automatically increment i each time it loops. Your current code will cause it to skip 1 every time it loops - so i will be 1, then 3, then 5 because it is automatically incrementing and then you are manually incrementing it further.

The second issue is that the entries in your table are not numerically keyed, they have explicit named keys (tekst1, tekst2 etc) but you are trying to index them as if they are numerically keyed. There is no vpr[1] to insert into a sceneGroup so that will always fail, as the entries in the table are all called vpr.tekst1 etc. For the same reason, #vpr will return 0 because there are no numeric indices in the table.

I can think of three ways to loop through the table:

  1. If you know how many entries there are, you could manually reference the table keys like so:
local numEntries = 9

for i = 1 , numEntries do
    sceneGroup:insert(vpr["tekst"..i])
end

This will loop from 1 to the value of numEntries, and each time will attempt to insert an entry from vpr which has the name "tekst"..i.
That is pretty crude, and relies on you knowing how many entries there are ahead of time.

  1. You could also use the in pairs() loop which does not care if keys are numeric, however it also doesn’t care about the order of entries and will run through the table in any order it likes:
for key, value in pairs(vpr) do
    sceneGroup:insert(value)
end
  1. Instead of looping through the table and adding them into the scene one by one, you could instead pass the sceneGroup into your vpr:vop() function and from add the items into the group as you create them:
local vpr = {}
function vpr:vop(parentGroup)

     vpr.tekst1 = display.newText( parentGroup, "8", display.contentCenterX - 10 , display.contentCenterY - 170, "MyriadPro-Regular.otf", 28 )
     vpr.tekst1:setFillColor(230/255,230/255,230/255,1)


     vpr.tekst2 = display.newText( parentGroup, "E", display.contentCenterX + 10 , display.contentCenterY - 170, "WebMobile-Regular.ttf", 56 )
     vpr.tekst2:setFillColor(132/255,0/255,50/255,1)

     --etc
end
local vpr = require("Questions.Quest" .. settings.TL )
local sceneGroup = self.view
vpr:vop(sceneGroup)
1 Like

Many thanks. I tried all 3 ways. The last one is the most convenient for me.

Glad the suggestions were of some help.

What I would say is that rather than just changing your code to use one of those suggestions, look into why they work. I’m not sure if you have experience with other languages, but your original code had a fundamental misunderstanding of both how Lua indexes tables and how it uses loops. You might struggle without having a good grasp on these concepts.

A good place to start is Solar2Ds Introduction to Lua:

Personally I think the original reference manual for Lua is one of the better written ones I’ve seen:
https://www.lua.org/manual/5.1/
It was written by the person who created the Lua language, so you can be pretty sure it will cover everything you’d ever need.

This topic was automatically closed 180 days after the last reply. New replies are no longer allowed.