Creating a module
Learn to code your own module
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.
The name of your module folder is very important since it will be used by the module system as your module identifier in the code, thus name it carefully.
I personally advise writing in lower-case (no issues on Linux servers), without spaces (easier access in code) and by adding a part of your username (a rule of thumb to avoid naming conflicts).
Example: guthscpkeycard
, guthscp096
, ctx096bag
...
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:
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:
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.
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
:
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