Question about performance using external modules or not

[quote=“dodi_games,post:19,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

Well, logically, speaking it is better to require those in a module so you call them from wherever instead of having to redefine them for each file. However, if it is only one file, I would assume that the difference in performance is very minuscule.

@sdktester15

Thanks for your response.  Do you think I need to code this:

package.loaded["myModule"] = nil

in my scenes “hide” or “destroy” to un-require myModule or when I call it at the top

local myModule = require( "myModule" )

and composer destroy is called “myModule” gets automatically un-require cause I set the module call to local?

Why do you need to un-require the module?

Rob

@rob

I think if I un-require the external module, which have 36 paints of 512x512 px is best for app performance.

I feel my device hot while running the App. I managed to drastically lower the texture memory using image sheets even for the background images. Now I’m looking for code optimization to see if I get more performance and my device runs good without heating.

52% of my images are big (512x512, 320x160, etc, and also use @2x images)

Maybe if I require the external module in main.lua and in the scene I will goin to use it is better? Then my app know there is an external module that I’m going to use in some scene. I’m just try to figure out how all interacts to deduce what is the best for my app.

I would like to use this topic to ask a related question. which of these three forms would be the best given the case.

36 paints to load to use in 36 different buttons touch events.

method 1

local paint1 = { type = "image", filename = "images/paint1.png" } local paint2 = { type = "image", filename = "images/paint2.png" } local paint3 = … to paint 36 local function paint1ButtonTouch( event ) if ( event.phase == "ended" ) then square.fill = paint1 end return true end ...36 buttons functions

method 2

local paint1, paint2 … to paint 36 local function paint1ButtonTouch( event ) if ( event.phase == "ended" ) then paint1={type="image",filename="images/paint1.png"} square.fill = paint1 end return true end ...36 buttons functions

method 3

local paint = {} local function paintButtonTouch( event ) if ( event.phase == "ended" ) then paint = nil paint={type="image",filename="images/paint1.png"} square.fill = paint end return true end ...36 buttons functions

thanks in advance
DoDi

Aaah no, it looks like AP Computer Science all over again!

I would assume option three would be better here since it takes out all your requires and makes the code more compact. However, as for performance, I am not too sure. @roaminggamer can take it away from here.

I presume you could use package.loaded[…] = nil on the “did” phase of hide() and the destroy() function.

How about measuring your fps when you use this line on the hide and destroy phases, and then when you don’t? Just to test this out.

FPS stay the same from my perception. Now, if I use the method one, scene load faster than using an external module.

I’m reading table tutorial in lua.org and I think if I use this method

local paints = { "paint1", "paint2", to paint 36...}

Then in the button function, “ended” phase, I assign the params to every “paint texture”, once assigned, fill the square, I would avoid loading all the textures when scene start. I also think that only the “paint texture” that the user selects when pressing the button will be loaded into texture memory. But I would like to be sure.

I would like to ask a question though, is each button going to set that square to a certain color and that’s it, or will there be more to each function, because if it is only setting a fill, then you could just make one function, and take in like an id or something, then you could concatenate the id to the file path.

Now if you are doing more with each of the 36 functions, than ignore what I said above, it is just a suggestion. :slight_smile:

Also, you said you tested with perception, did you do any FPS calculations?

I have a memTest clock, external module, from github that print FPS, texture memory and lua memory. I require it in main lua. I can not see any change when using any of the three methods.

I want to fill a square with 36 different paint textures. I have 36 buttons and 36 functions. I do not have the knowledge to make one button/function to make it all work at once. If you like to help me I will appreciate.

Alright, are the 36 button functions you use, are they similar to each other? If they are not, then we can’t make them all work from one function, but if they are we could.

For example post the code for button function 4, button function 19, and button function 34. Just as an example to verify if they are similar or not.

Yes, they are basically the same function, the only thing that changes is the square.fill and some extras. But all the buttons are in a scrollview. The button has “moved” and “ended” phase where in the “moved” phase I use it for the scrollview take focus and in the “ended” phase I play the sound of the button, move the position of two display objects (different position coordinates in each function) and use the square.fill. I’m not in my house but when I arrive I will post the code of the three functions that you ask me.