What does local widget = require("widget") do?

What more specifik does local widget = require(“widget”) really do?

It loads the widget library (like any library) and returns the module as a variable, in this case called ‘widget’. This allows the widget library to be used in Lua.  Using that same line of code in any module will then return the same library.

Thank you. What is a module?

A module is a lua file. Any file you create which you then load into memory using ‘require(…)’ is called a module.

Remember: A module is only loaded once and that means any call to require() will return the same value. This is why developers use this format in their module code:

local lib = {} -- do stuff return lib

Every time the developer calls ‘require(…)’ the same lib is returned because the module is only executed once. You can still call functions in that module many times, of course, but the local and the return statements in that file (and any other global code) will only execute the first time the file is loaded.

The only time a module is executed a second time is when it is deliberately unloaded, but I’m not going to go into that.

It loads the widget library (like any library) and returns the module as a variable, in this case called ‘widget’. This allows the widget library to be used in Lua.  Using that same line of code in any module will then return the same library.

Thank you. What is a module?

A module is a lua file. Any file you create which you then load into memory using ‘require(…)’ is called a module.

Remember: A module is only loaded once and that means any call to require() will return the same value. This is why developers use this format in their module code:

local lib = {} -- do stuff return lib

Every time the developer calls ‘require(…)’ the same lib is returned because the module is only executed once. You can still call functions in that module many times, of course, but the local and the return statements in that file (and any other global code) will only execute the first time the file is loaded.

The only time a module is executed a second time is when it is deliberately unloaded, but I’m not going to go into that.