Developing Plugins
Plugins are modular extensions to the schema that add or modify the gameplay of your gamemode. While it is possible to write all your code within the schema folder, using plugins is recommended as it allows you to easily add, modify and/or remove organized code.
There are 2 ways to create a plugin in NutScript:
- Single-file plugins, for small plugins such as Permakill.
- Folder based plugins, for bigger plugins that use items, languages, entities or derma, such as Vendor.
Single File Plugins
- Create a new
.lua
file in the plugins folder. Name the file whatever the plugin's name is. (e.g.permakill.lua
).
Folder Based Plugins
- Create a new folder in the plugins folder. Name the folder whatever the plugin's name is. (e.g.
vendor
). - Create a new file within the plugin folder. Name it
sh_plugin.lua
.
Starting
In your plugin/sh_plugin.lua
file, include the following:
Here you'll need to set your plugin's name, the author, and a short description of what it does.
Hooking
Normally, to hook a function, you'll need to use the following syntax:
However, NutScript has a special syntax for hooking functions within plugins.
Why PLUGIN?
The PLUGIN prefix allows us to tell NutScript that we are extending the functions we are going to use, rather than completely replace them with the ones we set inside the file. This prevents code from being removed from execution and therefore breaking some aspects of the gamemode or other plugins.
Advantages of using PLUGIN:
-
It gives the hooked code priority when executed. Meaning that code added via PLUGIN will run before most code added by addons.
-
It allows you to use code defined by PLUGIN to be run elsewhere in the gamemode.
Example
If you have a plugin
permadeath.lua
that defines a function calledPLUGIN:PlayerDeath(client, inflictor, attacker)
, and another plugin calledmedical
, you can have a function in themedical
plugin call thepermadeath
PlayerDeath hook without running any other code that has been hooked to PlayerDeath, vianut.plugin.list.permadeath:PlayerDeath(client, inflictor, attacker)
. -
It allows to store variables specific to the plugin.
- For example
is a table only available within the file/scope where it was defined.
However
is a table that is available (within the respective realm) to all files/scopes within the plugin, as well as accessible from other plugins (
nut.plugin.list.permadeath.curEnts
).
Example
An example of a plugin creation can be found here.
Tip
It is recommended that you use plugins as puzzle pieces that fit into your gamemode. Creating plugins not only makes it easier to develop and enhance sections of your gamemode, your schema will remain organized as it gets larger.