Why am I unable to add objects to the dedicated display group using a 2nd independent function within linedraw.lua?
--linedraw.lua
function LD.drawDot2()
local dot2 = display.newCircle(0,0,25 ) ; dot2:setFillColor(255,200,80)
dot2.x = math.random(-32,352) ; dot2.y = math.random(-44,524)
putInGroup:insert(dot2)
end
LD.drawDot2()
I get: “attempt to index upvalue ‘putInGroup’ (a nil value)”.
In my testing, it appears that only one function at a time can access the passed display group.
That is, unless one tries to fire LD.drawDot2 from level1.lua.
I’d really like it if all functions within linedraw.lua were aware of the passed display group, so that I could work within linedraw.lua as well.
Idea:
I guess I could wrap the entire module in a master function and pass the display group into there, but there has to be a better way. [import]uid: 73951 topic_id: 35817 reply_id: 144373[/import]
Hi Mort,
Looks like that’s a tiny error… that argument is not needed. I must have put it in there thinking I’d pass a group to the “drawDots” function directly, but in fact, I just start the touch event listener.
Small request: would you mind editing the code above to fix the tiny error? I’ve found that it’s helpful to people who may use this thread as a reference later on. @EHO: See this thread for an explanation of the reference LD.startListener = startListener : http://www.coronalabs.com/blog/2011/09/05/a-better-approach-to-external-modules/ [import]uid: 73951 topic_id: 35817 reply_id: 143640[/import]
@Brent So, is it safe to assume that if one removes MyGroup from within level1.lua, said generated objects passed through linedraw.lua will be freed from memory (excluding those already removed)?
Hi @mort,
When I tested this, the display objects added to “myGroup” are removed properly, and from memory. Of course, standard cleanup processes still apply, like canceling timers or transitions, removing them from any external “reference tables”, etc.
On that note, I just updated my code sample and took out the fade transition on the “dots”… mostly to make this clearer and not pollute this core process with an aspect that some people won’t need.
Hi @mort,
With the modification, try putting some objects in the group (in “linedraw.lua”) and then, from “level1.lua”, remove the group… those objects should be deleted and memory cleared. If they’re not, let me know and I’ll test some things out memory-wise on my end.
Great. Now, since auto-cleanup is gone (within linedraw.lua), how would one go about removing said lines using level1.lua?
My approach
Passing group to linedraw.lua to capture created objects (We’ve done this, right?). ?
Destroying said objects whilst in level1.lua. See where I’m going? I’d like to pass arguments through external modules for object creation, whilst managing the removal of objects from my original level module.
Think of the modules such as linedraw.lua as filters through which I run data.
Yes, that’s the idea. I can test this later tonight to ensure the memory is clearing properly… [import]uid: 200026 topic_id: 35817 reply_id: 143814[/import]
Much appreciated, I’ll get back to you if I have any other concerns as well! Thanks again. I’m just trying to make a reliable template/structure for managing objects. I don’t like having duplicate code in every single one of my modules, too much room for error. Let me know if there are any concerns one may have when it comes to upvalues and external references that may impeded object cleanup. That’s another thing that’s bugging me. [import]uid: 73951 topic_id: 35817 reply_id: 143816[/import]
Hi @mort,
How is this coming along for you? Did you find a better solution? I have one idea that I tested briefly which basically entails creating a “instantiate display group” function within “linedraw.lua”… essentially it just sets up the display group you send to the module, and then other functions can utilize it. This seems to work, but I’m not fond of how “limited” it seems… meaning, it sort of locks you into using just one display group, etc.
I hope to get some time to experiment with this further next week and make it more expandable and better, but I’m curious what you came up with in the meantime…
I believe I found a solution, let me know if this is proper Coronaese/Coronese:
First, within linedraw.lua, I create a display group (to hold all objects for linedraw.lua) and attach it to the LD table:
--linedraw.lua--
local LD = {}
...
local function startListener()
print("starting listener...")
Runtime:addEventListener("touch", drawDots)
---Create display group:
LD.linedrawgroup = display.newGroup()
end
...
return LD
Then…
I pull said group into my level1.lua and localize it, like so! :
--level1.lua
--Import display group from linedraw.lua and localize:
local LDgroup = LD.linedrawgroup
Bonus:
…and then all I have to do for easy clean up, is put said group into a master group within level1.lua!
[import]uid: 73951 topic_id: 35817 reply_id: 144930[/import]
Hi @mort,
I just tested out my module (from the original code) and the memory is clearing properly when the group is removed. I see you’re adding more functionality to it, like canceling the timers and such, and that’s fine.
I would suggest you localize your variables and functions though, i.e. myGroup, function “removeAll”, functions “ld.startListener” and “ld.endListener”, etc. I just stay away from global entirely now… I suppose some globals are OK, but I do everything within my power to avoid them.
If in doubt, you should try to just check your memory in the routine, and see how the numbers print out over time. This is the function I use:
local function garbagePrinting()
collectgarbage("collect")
local memUsage\_str = string.format( "MEMORY = %.3f KB", collectgarbage( "count" ) )
print( memUsage\_str, "TEXTURE = "..(system.getInfo("textureMemoryUsed")/1000000) )
end
timer.performWithDelay( 1000, garbagePrinting, 0 )
I thought the LD.startListener/LD.endListener, using my method, are localized into the table LD? [import]uid: 73951 topic_id: 35817 reply_id: 143963[/import]
Why am I unable to add objects to the dedicated display group using a 2nd independent function within linedraw.lua?
--linedraw.lua
function LD.drawDot2()
local dot2 = display.newCircle(0,0,25 ) ; dot2:setFillColor(255,200,80)
dot2.x = math.random(-32,352) ; dot2.y = math.random(-44,524)
putInGroup:insert(dot2)
end
LD.drawDot2()
I get: “attempt to index upvalue ‘putInGroup’ (a nil value)”.
In my testing, it appears that only one function at a time can access the passed display group.
That is, unless one tries to fire LD.drawDot2 from level1.lua.
I’d really like it if all functions within linedraw.lua were aware of the passed display group, so that I could work within linedraw.lua as well.
Idea:
I guess I could wrap the entire module in a master function and pass the display group into there, but there has to be a better way. [import]uid: 73951 topic_id: 35817 reply_id: 144373[/import]
Hi @mort,
How is this coming along for you? Did you find a better solution? I have one idea that I tested briefly which basically entails creating a “instantiate display group” function within “linedraw.lua”… essentially it just sets up the display group you send to the module, and then other functions can utilize it. This seems to work, but I’m not fond of how “limited” it seems… meaning, it sort of locks you into using just one display group, etc.
I hope to get some time to experiment with this further next week and make it more expandable and better, but I’m curious what you came up with in the meantime…