Why local widget = require( "widget" ) for widgets, all the other libraries don't need it

Hi,

why do I need to use 

   local widget = require( “widget” )

If i use the display or network library, no local ref is needed.

Please explain why the difference. Using a local references doesn’t work for me in some circumstances.

Thanks

Frank

The widget library is just a Lua Library that uses “display” functions to make heavily modified display objects with various behaviors(buttons, scrollview, etc.) To conserve Lua memory, Corona Labs decided to simply make people require the Lib if they need it. If you want it to be global simply do:

\_G["widget"] = require("widget") -- Access via Ex: local button = widget.newButton(...) -- Or local button = \_G["widget"].newButton(...)

or

widget = require("widget") -- Access via Ex: local button = widget.newButton(...)

the “_G” table is a Lua table where ALL global variables are stored and immediately inserted into even if declared as a regular “non local” variable. Remember, locals are faster to access but need to be be present in every Lua file/function that requires it. However, performance shouldn’t be an issue in accessing the library if its a global as you are probably using it when you are loading scenes and not during gameplay.

If we included all the libraries in the core, your apps would be very large.  Many people will never use widgets.  Many people will never use JSON.  Facebook, Storyboard, and a countless number of others have to be “required”.  Network is reasonably light weight and while it’s also not used by everyone, it’s used by enough of the other libraries that it is included for you.

You can do:

widget = require(“widget”)

in your main.lua leaving out the “local” and it would be available in all your modules and scenes without having to require it everywhere.  So why not do this?  Well it becomes a “global” and globals are pretty expensive on processing the way that Lua manages its resources internally (the way memory is accessed).  It is preferable to localize them using the:

local widget = require(“widget”)

in each module where you need it.  Keep in mind, the module is actually only required once and it’s main chunk code executed once, regardless of how many times you require it.  It is in effect globally available without the headache’s of being "global’.   That means in your main.lua you can do:

local fred = require(“fred”)

and assuming fred returns a table or object like most modules do, you could do:

fred.someAttribute = “someValue”

then in another module, you can do;

local fred = require(“fred”)

print(fred.someAttribute)

and you will see the value print successfully.  In this case, you could tack variables on to the widget object and pass them between scenes.  People using storyboard use this technique all the time to pass data around.

The widget library is just a Lua Library that uses “display” functions to make heavily modified display objects with various behaviors(buttons, scrollview, etc.) To conserve Lua memory, Corona Labs decided to simply make people require the Lib if they need it. If you want it to be global simply do:

\_G["widget"] = require("widget") -- Access via Ex: local button = widget.newButton(...) -- Or local button = \_G["widget"].newButton(...)

or

widget = require("widget") -- Access via Ex: local button = widget.newButton(...)

the “_G” table is a Lua table where ALL global variables are stored and immediately inserted into even if declared as a regular “non local” variable. Remember, locals are faster to access but need to be be present in every Lua file/function that requires it. However, performance shouldn’t be an issue in accessing the library if its a global as you are probably using it when you are loading scenes and not during gameplay.

If we included all the libraries in the core, your apps would be very large.  Many people will never use widgets.  Many people will never use JSON.  Facebook, Storyboard, and a countless number of others have to be “required”.  Network is reasonably light weight and while it’s also not used by everyone, it’s used by enough of the other libraries that it is included for you.

You can do:

widget = require(“widget”)

in your main.lua leaving out the “local” and it would be available in all your modules and scenes without having to require it everywhere.  So why not do this?  Well it becomes a “global” and globals are pretty expensive on processing the way that Lua manages its resources internally (the way memory is accessed).  It is preferable to localize them using the:

local widget = require(“widget”)

in each module where you need it.  Keep in mind, the module is actually only required once and it’s main chunk code executed once, regardless of how many times you require it.  It is in effect globally available without the headache’s of being "global’.   That means in your main.lua you can do:

local fred = require(“fred”)

and assuming fred returns a table or object like most modules do, you could do:

fred.someAttribute = “someValue”

then in another module, you can do;

local fred = require(“fred”)

print(fred.someAttribute)

and you will see the value print successfully.  In this case, you could tack variables on to the widget object and pass them between scenes.  People using storyboard use this technique all the time to pass data around.