Creating a module

Learn to code your own module

This tutorial assumes you are familiar with coding in Garry's Mod and creating addons.


Setting up the file structure

Consider the following as your addon's module file structure:

lua/
└── guthscp/
    └── modules/
         └── <module_id>/
             ├── <*/>      --  optional folders containing lua files
             ├── <*.lua>   --  optional lua files
             └── main.lua  --  the entry point

You have to create a folder in lua/guthscp/modules/ and its main.lua file where all the module informations ─ such as the author name, version, file requires, dependencies, the configuration, etc.. must be defined.

You can then optionally create as many .lua files and folders as you want and freely organise them. As everything is parented to your unique module folder, you don't have to worry about conflicting file names.


Defining the module

Open your main.lua and start by creating a MODULE local table which you return at the end of the file as follow:

main.lua
local MODULE = {}

return MODULE

The MODULE table is what defines your module, it will contains various informations like, as stated before, your author name, the module version, files & folders to includes, the dependencies, etc..

Now, you have to fill your MODULE table with the required properties:

main.lua
local MODULE = {
    name = "My Module",
    author = "Me",
    version = "1.0.0-dev",
    description = "My module is awesome! :)",
    icon = "icon16/new.png",
}

--  ...

This is the bare minimum to make a module register through the module system and to see it in the guthscpbase menu. Of course, edit them as you'll like.

For more information about the module class, read this page.

Your module is now registered in the guthscp.modules table. You may want access to it (for instance in your other scripts files) by using guthscp.modules.<my_module_id>.


Enabling Hot-Reload

As you are developping your own module, you may want to do changes to your files without having to reload the server or your game.

This is really simple, you just have to call the function guthscp.module.hot_reload before the return MODULE:

main.lua
local MODULE = {
    --  ...
}

--  ...

guthscp.module.hot_reload( "<my_module_id>" )  --  enabling hot reload
return MODULE

Now, each time you want to reload your module properties, dependencies, configuration or requires in-game, you just have to save your main.lua (gmod's hot reload will run the file and so this function will reload the entire module).


Last updated