Do Lua tables contain pointers, copies, instances, or references of objects that you add to them? and Display Image issues

I am coming from a python background so I keep trying to work in a pythonic way. That could be my issue, let me know.

My issue at hand is this, I need to make 15 display.newImage calls to load in 15 specific 512x512 images. The paths to these images is generated by a pair of for loops that operate on a pair of arrays. Something like this {1,2,3,4,5} and {“a”,“b”,“c”} which get me 1a,1b, ect…

The trouble I am running into is that while I can get Lua to add each new display.newImage to a single array use table.insert() if I do this in the main.lua I get some results, if I do it in a module and pass it back then I don’t get anything visual to display.

I have also not been able to add them to a two dimensional array. Ideally I could use two string keyboards as the ids. In python this would be a Dictionary with Dictionaries inside it.
I could call it like this:
foo[‘bar’][‘bob’]
Where foo is my table/dict and ‘bar’ and ‘bob’ are string indexes.

When I try and make a structure like this in lua and then latter try and call them things like .isVisable return as nil

However if I make them a 1d array with just int indexes and use table.insert() then .isVisable works.

The final app will only display 1 of the 15 images at a time, it will need to switch between them quickly based on user input from the accelerometer and touch.

I’m trying to port this from GameSalad where I made it in about 3 hours.

I had planned to not use a sprite sheet due to the number of images and size that I am loading into memory.

I had originally made the function that generated all display objects in a separate lua file then had it just return the array. The problem is none of the images would ever display, once the function just returned a list of paths and I did all my display.newImage in main.lua the graphics could appear. Do Lua tables contain pointers, copies, instances, or references of objects that you add to them? I wanted to pass back the table and 15 images from the module to the main code.

So after reading all that, am I going about this in the right method for Corona? Is there a better structure to building this type of app here?

Ideas I have toyed with:
* Making n DisplayGroups and using them as dimensions
* Creating a lookup between the int index and my 2d system
* creating a helper function that just takes in what should be on right now
* having a global variable for each of my 2 look ups and have the user input set those in separate functions , then have enterframe event turn all the right ones on and off

[import]uid: 110373 topic_id: 19364 reply_id: 319364[/import]

So just to confirm your basically trying to load and store (+ display) 15 images into a 2d table yes? [import]uid: 84637 topic_id: 19364 reply_id: 74750[/import]

That would be an accurate and much shorter way of putting it Danny, yes. :smiley: [import]uid: 110373 topic_id: 19364 reply_id: 74751[/import]

Great :slight_smile:

How about this (untested)

–In lua you have to create the tables structure before being able to fill it (for 2d arrays)

  
--imgTbl = a table of image names (and there location if nesaccary)  
-- Ie :   
  
local myImgTbl = {  
 {"images/img.png"},  
}  
  
local function generateNew(imgTbl, r, c)  
 local newTbl = {}  
  
 --Set up the 2d array based on r (rows) and c (columns)  
 for j = 1, c do  
 newTbl[j] = {}  
 for i = 1, r do  
 newTbl[j][i] = 0  
 end  
 end  
  
 --Load images into table  
 for i = 1, #imgTbl do  
 for j = 1, #imgTbl do  
 newTbl[i][j] = display.newImage(imgTbl[i])  
 end  
 end  
  
 return newTbl  
end  
  
--Call it  
generateNew(myImgTbl, 10, 10)  

Hope that helps, if I understood your request correctly [import]uid: 84637 topic_id: 19364 reply_id: 74755[/import]

Danny,
Thanks I will give that a shot, it looks like my main mistake is that I was trying to do the whole thing inside one set of for loops.

Wouldn’t I need to save the results of the function call at the end to a new var?

[lua]local myNewImages = generateNew(myImgTbl, 10, 10)[/lua]

If generateNew is inside a module that is a separate lua file would the returned newTbl and the display.newImage’s inside it work in the main.lua ?

That was the other problem I had.

-Brad [import]uid: 110373 topic_id: 19364 reply_id: 74759[/import]

Yes on all accounts :slight_smile: [import]uid: 84637 topic_id: 19364 reply_id: 74761[/import]