Loading and using modules

You can load Lua modules with the require function. The require function takes one argument, which is the name of the file being loaded (without the .lua extension). The require function should return a table that can then be used as any other table.

The require function searches for the given file in several specific folder paths. These paths are stored as a string in the global variable, package.path. The default path on windows looks like this:

?;?.lua;c:\windows\?;/usr/local/lua/?/?.lua

This string contains multiple search paths, each separated by a semicolon (;). The ? charter gets replaced by the filename provided to the require function. So, for example, if you called require("character"), Lua would look for ...

Get Lua Quick Start Guide now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.