Question about performance using external modules or not

The button function:

local 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 paint={type="image",filename="images/paint1.png"} audio.play( a\_1) select.alpha = 0 select.x = 21 select.y = 35 transition.fadeIn( select, { time=350 } ) square.fill = paint end return true end

I do not write the other functions cause they are all the same, the change are in x and y values of “select” display object and paints texture images.

Thanks in advance

DoDi

Or maybe call the paints local inside every button function, “ended” phase, works better?

I think there may be some confusion.

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.

Rob

Thanks Rob, very well explained. Last question, is there any difference between these two methods?

local paints = { "paint1", "paint2", "paint3" } paint1 = { type = "image", filename = "images/paint1.png" } paint2 = { type = "image", filename = "images/paint2.png" } paint3 = { type = "image", filename = "images/paint3.png" }

or the one you suggest: 

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"

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.

any help?

Thanks DoDi 

@nick_sherman

I read in the forums or a tutorial, don’t remember right now, to avoid the 60 up values issue you can do the empty table.

Is something like store a bunch of variables in to one local table to make them local too, then anywhere in the code, use those variables. 

I’m right?

based on the code as you have it in paint.lua

in scene1.lua

it should be   paint[1]     not  paint.paint[1]

square.fill = paint[1]

Best of luck with your game!

[quote=“dodi_games,post:47,topic:348153”]

@nick_sherman

[font=‘MyriadPro-Regular’]

I read in the forums or a tutorial, don’t remember right now, to avoid the 60 up values issue you can do the empty table.

[/font]
[font=‘MyriadPro-Regular’]

Is something like store a bunch of variables in to one local table to make them local too, then anywhere in the code, use those variables.

[/font]
[font=‘MyriadPro-Regular’]

I’m right?

[/font] [/quote]

Yeah, but you’re not doing it right. You need to do:

[lua]

local paints = {}

paints.paint1 = …
paints.paint2 = …

[/lua]

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] 

@nick_sherman

ok I understand…

but if i want to call paints from an external module?

I try this:

--paint.lua local M = {} M.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" } return M ------------------------------------------------------------- --scene local paint = require( "paint" ) --button "ended" phase   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]     --error global something   end   return true

Fix paint.lua:

[lua]

local M = {}

M.paint = {}

M.paint[1] = { type = “image”, filename = “images/paint1.png” }
M.paint[2] = { type = “image”, filename = “images/paint2.png” }
M.paint[3] = { type = “image”, filename = “images/paint3.png” }

return M

[/lua]

In other scene:

[lua]

local myExternalModule = require (“paint”)

square.fill = myExternalModule.paint[1]

[/lua]

@dodi_games

My original comment was correct… based on the code in your previous example.    paint[1]     not  paint.paint[1]

now, since you changed your paint.lua module  the answer is now different

now the  paint.paint[1] will work,

BUT,

you need to first put a ‘M.’  before each paint[] in the paint module

M.paint[1] =

M.paint[2] =

etc…

best of luck!

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.

thanks again

DoDi

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

It’s just a table.

Rob

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”?

Thanks
DoDi