Question about lua Modules (SOLVED)

Hello everybody! I have a quick question about how lua modules work. Here is an example of my module:

local module = {}

– a load of functions

return module

In a separate script I then call ‘require’ on the module and call the functions when needed.

My question is, given that the code in the module ends with ‘return module’, does that mean the entire contents of the module are returned to my separate script every time I call one of the module’s functions, or does this happen just once when it I call ‘require’ on the module?

If it’s the former, then I’m thinking lots of small modules might be better than one big module in terms of speed.

Many thanks. :slightly_smiling_face:

The module is loaded once and then cached. You will get the same value back every time.

1 Like

Ok that makes sense. Thank you.

More info here: https://defold.com/manuals/modules/#requiring-lua-files

Ah yes - thanks. I think I’m beginning to get my head around it now. The documentation for Defold is really great.

1 Like