local paint1 = { type = "image", filename = "images/paint1.png" }
Creates a table with two string elements in it. It’s using zero texture memory.
local paint = {} paint[1] = { type = "image", filename = "images/paint1.png" paint[2] = { type = "image", filename = "images/paint2.png"
would create a table of sub-tables. Texture memory isn’t allocated until you update the “.fill” attribute.
So now the question is 36 tables more efficient than one table with 36 sub-tables. I would say it would be purely on memory consumption, the 36 individual tables would save a few bytes of memory. But Lua only allows you to have 200 local variables and using the 36 takes a chunk of those. Then you have the concept of upvalues, which are where you, inside a function reference a value at a scope higher than that function. You can only have 60 upvalues referenced in any one Lua block, so if you in your function write:
local function button( event ) if id == 1 then square.fill = paint1 elseif id == 2 then square.fill = paint2 else id == 3 then square.fill = paint3 end end
Your reference to paint1, paint2 and paint3 in this example are upvalues. Since you will have 36 of them, you’ve used 36 of your 60 upvalues. If you do a table of sub-tables, you use 1 upvalue. So from that perspective, I would suggest a table of sub-tables. Then you can use a simple index to access it. A table of sub-tables is easily put into an external module.
Well, the top one wouldn’t even work as you think it would. Paints is a table containing just three string values, and paint1, paint2 etc are standalone global tables with nothing to do with paints.
After reading and testing what is spoken in this topic I would like to carry out the code by making a table in an external module to be able to use it in the required scene but I am having scope problems.
--paint.lua local paint = {} paint[1] = { type = "image", filename = "images/paint1.png" } paint[2] = { type = "image", filename = "images/paint2.png" } paint[3] = { type = "image", filename = "images/paint3.png" } --to paint[36] return paint
but when I try to use it in scene give a local/global error
--scene1.lua local paint = require( "paint" ) local function paint1ButtonTouch( event ) if ( event.phase == "moved" ) then dy = math.abs( event.y - event.yStart ) if ( dy \> 5 ) then scrollView:takeFocus( event ) end elseif ( event.phase == "ended" ) then audio.play( a\_1) select.alpha = 0 select.x = 21 select.y = 35 transition.fadeIn( select, { time=350 } ) square.fill = paint.paint[1] --here is the error end return true end
The reason is I don’t know how to handle table in external module.
I think this way is the best performance using external modules.
I use paint.paint1 cause paint.lua is an external module. If I’m going to call paint[1] from an external module I must use paint[1] instead of paint.paint[1]
I’ve been looking since yesterday to improve the performance of the app. Thanks @nick, I do not know how I did not see that, reading in Corona docs, lua.org, lua wiki, tends to confuse a little. For a newbie it is difficult at first time to adapt and understand those things that are simple for you.
I’m going to implement that in the right way and I’ll be writing how the app’s performance improves or not.
Lua modules are just tables. When you require paint and paint.lua returns an indexed by number table like you’re doing here, then you get an indexed table.
Your button function would become:
local paint = require( "paint" ) local function paint1ButtonTouch( event ) if ( event.phase == "moved" ) then dy = math.abs( event.y - event.yStart ) if ( dy \> 5 ) then scrollView:takeFocus( event ) end elseif ( event.phase == "ended" ) then audio.play( a\_1) select.alpha = 0 select.x = 21 select.y = 35 transition.fadeIn( select, { time=350 } ) square.fill = paint[1] end return true end
I was reading the documents and I realized I can do “object.fill” from an “image sheet”. Because the topic is about performance, it would be a good idea to place my “image sheet” in the external module to use it for the “object.fill”?
It makes me ask cause the external module would read the information of the module that the texturepacker gives me, then the use of the “paint” would need information of two modules.
I ask if this would be a good scenario for the use of “images sheets”?