2.7 KiB
Getting started
If you want to learn how Lua works and the syntax, you can check these websites: https://www.lua.org/manual/5.2/ https://www.tutorialspoint.com/lua/lua_overview.htm
How mods are executed
When the server finishes loading everything, Lua For Barotrauma starts up and reads the file Lua/LuaSetup.lua and executes it, this Lua script registers classes to be available on the Lua side, creates static references and puts them in the global space, and then executes the mod's autorun folder. By default it only executes mods enabled in the settings menu, you can bypass that by editing the file LuaSetup.lua and setting local runDisabledMods = false to local runDisabledMods = true.
Creating your first mod
When creating a new Lua mod, you will need to create a new folder on the Mods folder, then inside this folder you will need to create a folder called Lua, and then inside the Lua folder you create a folder called Autorun, inside this folder you can add your Lua scripts that will be executed automatically, it will look something like this Mods/MyMod/Lua/Autorun/test.lua, remember that if you are using the default LuaSetup settings, your mod will only be executed if its enabled in the game's settings, so you will need to create a filelist.xml as unusual.
Now you can open test.lua in your favorite text editor (vscode recommended) and type in print("Hello, world"), now start the server by hosting a game or running the server executable manually, you will see in your console a text appear with the text that you entered in print, you can now type in the server console reloadlua, this will re-execute all the Lua scripts. You can also type in lua yourscript to run a short lua snippet, like this lua print('Hello, world'), remember to remove double quotes, because Barotrauma console automatically formats those.
Learning the libraries
In the sidebar of the documentation, you can see a tab named Code, in there you can check out all the functions and fields that each class has, each one of them has a box with a color on it, where means Server-Side, means Client-Side and means both Server-Side and Client-Side, by clicking on them you can learn more about them. Not everything is documented here, theres stuff missing that still needs to be added, if you want to find more in-depth functions and fields in the Barotrauma classes, you should check the Barotrauma source code.