# Lua Modules

Lua modules are useful for sharing functions and data between your scripts.&#x20;

To be able to share data between scripts, make sure you have "shared state" checked in your game.project file.

You can create a Lua module by right clicking on the asset panel and going New -> Lua Module you can then name this file and create it.

This is just a .lua file which can be created with other tools as well.

The default Lua module is blank with some comments only.

The standard pattern for a Lua module file is as follows:

{% code title="mymodule.lua" %}

```lua
local M = {}

return M
```

{% endcode %}

The standard way to include a Lua module in another script is as follows:

```lua
local mymodule = require("mymodule")
```

If you had your mymodule.lua file within a utils folder then your include would be:

```lua
local mymodule = require("utils.mymodule")
```

We will edit our Lua module to add a function and a variable.

{% code title="mymodule.lua" %}

```lua
local M = {}
M.secret = 777

-- you can get and set the M.secret value outside of the module

function M.hello_world()
    print("Hello World Defold!")
end

-- you could call M.hello_world() here once too
-- if you wanted any function to be called once for your module
-- such as an init function which checks to see if it
-- has already been initialized or not

-- you can also define local functions here
-- local functions in a module are private to that module

return M
```

{% endcode %}

Then in a script we can include and use the module's function:

{% code title="main.script" %}

```lua
local mymodule = require("utils.mymodule")

function init(self)
	print(mymodule.secret)
	mymodule.secret = 25
	print(mymodule.secret)
	mymodule.hello_world()
end

```

{% endcode %}

```
DEBUG:SCRIPT: 777
DEBUG:SCRIPT: 25
DEBUG:SCRIPT: Hello World Defold!
```

{% file src="<https://59336108-files.gitbook.io/~/files/v0/b/gitbook-legacy-files/o/assets%2F-LHnmfJK3HDz-s0f74qO%2F-LJPbui2o8ngG4uUlRVk%2F-LJPfu6aRNEw5yySiLa7%2FModuleExample.zip?alt=media&token=d7062d35-8b61-414f-b748-a992e3866304>" %}

If you look through the source of assets published on the community portal most of them are Lua modules which follow this similar pattern.

If you wish to share your module then you'll want to give it a catchy name, then put it inside of a folder with that name. For example "log/log.lua" see here <https://github.com/subsoap/log>

Then in the game.project file you would define the "log" folder as a include dir in the library area. See Log's setup for an example of this.

Sharing modules on Github is currently the most popular way of sharing modules. Again, see how others are done and copy them. There are still many resources which have no Defold version so there is still opportunity for you to contribute to the community! When you do publish something make sure you use a license which has as much freedom for its users as possible if you want people to actually use it. If you want to put your work into the public domain like I generally do then you can use the **Creative Commons Zero v1.0 Universal** license.


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://read.bookofdefold.com/defold-a-z/lua-tips/lua-modules.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
