optimization with functions

Hello, I have a pretty important question about which method is more efficient/optimized/run faster in my app,

First:

Enter scene…

Don’t use functions

-Load up spritesheets, npc data, etc

-Create NPC

-Create NPC

-Create NPC

This option has a lot of repeated code in the create NPC, making it very long…but only loads up spritesheet once.

Second:

Function spawner (params)

    - load spritesheet

    - npc, monster, etc on params data

    - create based on params

    - return object

end

Enter scene…

  • create NPC ( Call function spawner,  pass in params, get NPC data back)

  • create NPC ( Call function spawner,  pass in params, get NPC data back)

  • create NPC ( Call function spawner,  pass in params, get NPC data back)

This option uses a lot less code as it gets rid of repeated code, and here is why I ask this question I am calling this function over and over when I want to spawn something/anything and it loads up spritesheet, monster data and spits back monster. I figured loading up the spritesheet data continously on the fly and everything would boggle the system down more?

My spritesheet data uses a lot of sheets and a lot of different sprite angle’s as its already 50+ lines of code just on the spritesheet initalization.

I care more about optimization with less lag, and gain more fps…I care less about readability.

Thanks for your time.

If they all use the same sprite sheet, write a function that uses the sprite sheet, but doens’t load it.  In the scene’s create scene, load the spritesheet, then call your function to do the spawning using the already loaded sprite sheet.

Rob

Should I be passing the actual sprite sheet variable, handler to the function also? That would be painful…

local spritesheet

local function spawnstuff()

    …

    doSomething-with-spriitesheet.

end

function scene:create( event )

    …

    spritesheet = code_to_create_spritesheet()

    spawnStuff()

end

thank you very much :slight_smile:

Look at the OOP method, it could help you to optimize and get cleaner code.

Can you send me link to the OOP method please? If you are talking about [Tips] Optimization 101 that’s where I got this idea from.

you have a lot of informations through the forum for the Object Oriented Program,

In reality OOP is just classes with functions that they could be called from anywhere and re-used by other object thanks to metatable, even if they are complicate to understand  :stuck_out_tongue:

you can get as well a library there are many made by dev to handle Classes/metatable easily.

Here is a nice tutorial with 30log_library:  http://coronalabs.com/blog/coronageek/corona-geek-107/

You can get some pretty cool advices from here with dmc_library :http://forums.coronalabs.com/topic/29505-advanced-oop-removeeventlistener-question/?hl=%2Busing+%2Bmetatable

Other good tuto  : http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

and here a basic tutoriel from corona: http://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

As you can see there are a lot of ways to be done with it. so find your way depending on your need and use the one you are the more comfortable

If they all use the same sprite sheet, write a function that uses the sprite sheet, but doens’t load it.  In the scene’s create scene, load the spritesheet, then call your function to do the spawning using the already loaded sprite sheet.

Rob

Should I be passing the actual sprite sheet variable, handler to the function also? That would be painful…

local spritesheet

local function spawnstuff()

    …

    doSomething-with-spriitesheet.

end

function scene:create( event )

    …

    spritesheet = code_to_create_spritesheet()

    spawnStuff()

end

thank you very much :slight_smile:

Look at the OOP method, it could help you to optimize and get cleaner code.

Can you send me link to the OOP method please? If you are talking about [Tips] Optimization 101 that’s where I got this idea from.

you have a lot of informations through the forum for the Object Oriented Program,

In reality OOP is just classes with functions that they could be called from anywhere and re-used by other object thanks to metatable, even if they are complicate to understand  :stuck_out_tongue:

you can get as well a library there are many made by dev to handle Classes/metatable easily.

Here is a nice tutorial with 30log_library:  http://coronalabs.com/blog/coronageek/corona-geek-107/

You can get some pretty cool advices from here with dmc_library :http://forums.coronalabs.com/topic/29505-advanced-oop-removeeventlistener-question/?hl=%2Busing+%2Bmetatable

Other good tuto  : http://www.omidahourai.com/from-zero-to-oo-ardentkids-guide-to-object-oriented-lua-with-corona-sdk

and here a basic tutoriel from corona: http://coronalabs.com/blog/2011/09/29/tutorial-modular-classes-in-corona/

As you can see there are a lot of ways to be done with it. so find your way depending on your need and use the one you are the more comfortable