Despite reading dozens of forum posts, numerous tutorials and relentless attempts, I cannot figure this out. I’ve caved and thought I’d come and see if any of you are able to assist.
Simply, I’m trying to write/read a subtable but I’m struggling to make it happen. Most forum posts show the data built in the table, but mine is built on the fly.
Without trying to post masses of code, I will try to keep it succinct.
Result: Either I get nil tables, or I can’t determine how to read the subtables in a loop later as explained below.
Input : The input table being read is a JSON table. This I can read just fine. When I get to the menuItem section, I then loop through each menu item. Below is the section with the issue.
"99345681": { "itemID": 99345681, "itemType": "\_menuSimple", "nbrItems": 3, "backgroundImageLocal": "defaultAssets/bgrnd.png", "backgroundImageRemote": "", "backgroundColour": "red", "headerLoadScreenID": "", "headerImageLocal": "defaultAssets/dftButton.png", "headerImageRemote": "", "headerImageWidth": 0, "headerImageHeight": 0, "menuItem": { "1": { "iconimagelocal": "defaultAssets/dftWebIcon.png", "buttonimageremote": "", "loadScreenID": "12345681", "iconWidth": 100, "iconHeight": 100, "itemText": "Map Click", "itemFont": "native.systemFont", "itemFontSize": 16, "itemSubText": "Map Click Wheee", "itemSubFont": "native.systemFont", "itemSubFontSize": 12 }, "2": { "iconimagelocal": "defaultAssets/dftWebIcon.png", "buttonimageremote": "", "loadScreenID": "12345680", "iconWidth": 100, "iconHeight": 100, "itemText": "Web Click", "itemFont": "native.systemFont", "itemFontSize": 16, "itemSubText": "Web Click wheee", "itemSubFont": "native.systemFont", "itemSubFontSize": 12 }, "3": { "iconimagelocal": "defaultAssets/dftWebIcon.png", "buttonimageremote": "", "loadScreenID": "12345681", "iconWidth": 100, "iconHeight": 100, "itemText": "No Click", "itemFont": "native.systemFont", "itemFontSize": 16, "itemSubText": "Text on item 3", "itemSubFont": "native.systemFont", "itemSubFontSize": 12 } }
Objective:
What I *want* to do is have something like…
I’m not going to go through ALL the different things I’ve tried, but it’s been a few! Its the table/subtable thats killing me. I think I managed to get them loaded into the table, but couldn’t figure out how to reference it again to get to the next step per below.
local \_ms = {} \_ms.menuItem={} \*note: tbl = the JSON shown above. for k, v in pairs( tbl ) do -- This works if k=="menuItem" then -- This works for items=1,\_ms.nbrItems do -- This works for m, i in pairs( tbl["menuItem"][tostring(items)] ) do -- This works \_ms.menuItem[#\_ms.menuItem+1].m=i -- This is the bit I can't figure out -- So I can refer to it later as... -- button[1].label = \_ms.menuItem[1].itemText -- button[1].image = \_ms.menuItem[1].iconimagelocal -- button[2].label = \_ms.menuItem[2].itemText -- button[2].label = \_ms.menuItem[2].iconimagelocal end end end
Later in another module I will have the below. (I’ll extrapolate to fix the remaining fields once one is working). The purpose being that I construct the button solely from JSON input.
button[i].label = display.newText( \_ms.menuItem[i].itemText, \_ms.menuItem[i].width, \_ms.menuItem[i].y,\_ms.menuItem[i].font,\_ms.menuItem[i].fontSize)
Extra fluff:
I AM aware of print_r and I’ve used it to prove that I was able to load the table somewhat, but I end up confusing myself at times. I also used the debugger to monitor on the fly to figure out where I was overwriting. Much of my later results was loading the table, but not incrementing it, which is why I’m currently at the ‘+1’ stage.
For the JSON above the menuItem, that works just fine as I use code similar to…
if (k=="backgroundImageLocal") then if v=="" then -- Set default or print error if needed else \_ms.backgroundImageLocal = v end end -- Later I used bgrnd = display.newImageRect(\_ms.backgroundImageLocal, display.actualContentWidth , display.actualContentHeight ) with success
I just need to be able to handle items where we have 1 to X number of menu items and be able to process them.
Appreciate any help you can offer. Tables are SUCH a pain when I get more complex with them
Thank you