Passing images between modules

Aim:

To pass a group and table data from scene1.lua into pList.lua module and have pList create display objects and groups which are inserted into the group passed from scene1.lua.

Question:

Is there a tweak to this process so that only scene1 contains the images not pList?

To clarify, the following code shows the process we are using. This results in the M table of pList still containing (either a reference or the actual image) between scenes that require pList. I’m aware that’s because the module pList is only required once so M is persisting between scenes, but is there a slightly different way of doing this that doesn’t result in M of pList containing the images as well as scene1?

Code:

-- scene1.lua local pList = require 'pList' local gp = {} local obj = {} local pageGroup = display.newGroup( ) local setup = { { group = "main" }, { name="fred", imgFile=dir.."fred", w=657, h=495, x=1472.5, y=351.5, }, { name="barney", imgFile=dir.."barney", w=973, h=746, x=1482.5, y=963, }, { name="bambam", imgFile=dir.."bambam", w=809, h=570, x=1481.5, y=875, }, { name="betty", imgFile=dir.."betty", w=346, h=324, x=1490, y=396, }, { name="wilma", imgFile=dir.."wilma", w=350, h=328, x=1492, y=396, }, } local params = ({list = setup, group = gp, pageGroup = pageGroup}) obj = pList:createObj(params)

Then pList =

-- pList.lua local M = {} function M:createObj(params) local params = params or {} local list = params.list local group = params.group local pageGroup = params.pageGroup local groupName for i = 1, #list do local obj if listCur.group ~= nil then groupName = listCur.group if group[groupName] == nil then -- Does not exist then create new Group --print(groupName.." Group didn't exist, creating") group[groupName] = display.newGroup() group[groupName].name = ("gp."..groupName) --print("group created = ", group[groupName].name) pageGroup:insert (group[groupName]) end end obj = display.newImageRect ( listCur.imgFile, listCur.w, listCur.h ) obj.x = listCur.x; obj.y = listCur.y group[groupName]:insert(obj) self[listCur.name] = obj end return self end return M

Comment:

When a new scene is created and scene 1 destroyed, the scene group takes care deleting those objects. The problem is that M from pList endures between scenes. That is, M still contains the images, or a reference to them between scenes. To avoid this, M can iterated through in pairs to delete such as:

for k, v in pairs (self) do
    if type(v) == “table” then
        display.remove(M[k])
        M[k] = nil
        k, v = nil, nil
    end
end

Is there a better way to achieve this? The groups and images still do have to be created in pList from tables and a master group passes in from scene 1 but is there a different way of achieving this so that the images don’t stay in pList. Perhaps another method for achieving this part:

local params = ({list = setup, group = gp, pageGroup = pageGroup})
obj = pList:createObj(params)
 

I don’t see how the objects are persisting. “obj” is local to the for loop and when the for loop ends, those local variables won’t exist any more so there are no references there. Both group and pageGroup are local to createObj and those local variables should get trashed when the function ends. I don’t see were you have any instances that persist.

Many thanks for your reply Rob. The code above has been reduced down so I missed an important line (now restored). That is, to place a key into M with name = listCur.name at the end of each list iteration.

self[listCur.name] = obj

Since then I’ve thought of another way to go (as yet untested). But I’d be interested to hear your thoughts.

I don’t see how the objects are persisting. “obj” is local to the for loop and when the for loop ends, those local variables won’t exist any more so there are no references there. Both group and pageGroup are local to createObj and those local variables should get trashed when the function ends. I don’t see were you have any instances that persist.

Many thanks for your reply Rob. The code above has been reduced down so I missed an important line (now restored). That is, to place a key into M with name = listCur.name at the end of each list iteration.

self[listCur.name] = obj

Since then I’ve thought of another way to go (as yet untested). But I’d be interested to hear your thoughts.