new documentation and some fixes in code

This commit is contained in:
Evil Factory
2021-09-12 15:58:59 -03:00
parent 3cd5a23af7
commit 656af7df2f
29 changed files with 1861 additions and 5 deletions
+14
View File
@@ -0,0 +1,14 @@
# 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.lua.org/manual/5.2/) [https://www.tutorialspoint.com/lua/lua_overview.htm](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/MoonsharpSetup.lua` and executes it, this Lua script then looks for Mods in the Mods folder, and tries to execute Lua scripts inside the Lua/Autorun folder, so for example, if you have a Mod named TheTest, and inside this mod you have a file named Lua/Autorun/test.lua, the test.lua will be executed automatically.
## 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`
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 (script) 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, and learn more about them, but 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.
@@ -0,0 +1,17 @@
## Installing Lua For Barotrauma
1 - Download [latest version of Barotrauma Lua](https://github.com/evilfactory/Barotrauma-lua-attempt/releases/download/latest/barotrauma_lua_windows.zip)<br>
2 - Extract the zip file, you should get a folder called barotrauma_lua <br>
3 - Find the Content folder in your original Barotrauma game: <br>
![](https://cdn.discordapp.com/attachments/799752463619325968/833120013149929492/unknown.png)
![](https://cdn.discordapp.com/attachments/799752463619325968/833120379378991104/unknown.png)
![](https://cdn.discordapp.com/attachments/799752463619325968/833120841277374464/unknown.png)
4 - Copy the Content folder to the barotrauma_lua folder <br>
![](https://cdn.discordapp.com/attachments/799752463619325968/833133217300742154/unknown.png)
5 - Done! Now run Barotrauma.exe to run the modded game <br>
## Updating
To update you only need to replace the DedicatedServer.dll file from the latest release.
+110
View File
@@ -0,0 +1,110 @@
# Lua Examples
```lua
Hook.Add('chatMessage', 'suicide_mod', function(msg, client)
if msg == '!suicide' and client.Character ~= nil then
client.Character.Kill(CauseOfDeathType.Unknown)
Game.SendMessage(client.name .. ' killed himself!', ChatMessageType.Server)
return true -- hide message
end
end)
```
```lua
local characters = Character.CharacterList
local biteWoundsPrefab
for k, v in pairs(AfflictionPrefab.ListArray) do
if v.name == "Bite wounds" then
biteWoundsPrefab = v
break
end
end
for k, v in pairs(characters) do
v.CharacterHealth.ApplyAffliction(v.AnimController.MainLimb, biteWoundsPrefab.Instantiate(100));
end
```
```lua
Hook.Add("itemApplyTreatment", "testItemApplyTreatment", function (item, user, character, targetlimb)
if item.name == "Bandage" then
local pos = character.WorldPosition
Game.Explode(pos, 1, 500, 5000, 5000, 5000)
Game.RemoveItem(item)
end
end)
```
```lua
-- for example: create an item in xml named RandomComponent and add the wiring inputs/outputs trigger_random and random_out
Hook.Add("signalReceived", "signalReceivedTest", function (signal, connection)
if connection.Item.name == "RandomComponent" and connection.Name == "trigger_random" then
connection.Item.SendSignal(tostring(Random.Range(0, 100)), "random_out")
end
end)
```
```lua
local discordWebHook = "your discord webhook here"
local function escapeQuotes(str)
return str:gsub("\"", "\\\"")
end
Hook.Add("chatMessage", "discordIntegration", function (msg, client)
local escapedName = escapeQuotes(client.name)
local escapedMessage = escapeQuotes(msg)
Networking.RequestPostHTTP(discordWebHook, '{\"content\": \"'..escapedMessage..'\", \"username\": \"'..escapedName..'\"}')
end)
```
```lua
local enabledPackages = Game.GetEnabledContentPackages()
local shouldRun = false
for key, value in pairs(enabledPackages) do
if value.Name == "MyContentPackage" then
shouldRun = true
end
end
if Game.IsDedicated then shouldRun = true end
if not shouldRun then
return
end
```
```lua
-- by jimmyl
Hook.Add("chatMessage","controlhuskcommand",function(msg, client)
if msg == "!controlhusk" then
if client.Character ~= nil then
if not client.Character.IsDead then
return true
end
end
if not client.InGame then
return true
end
local chars = Character.CharacterList
local suitablechars = {}
for i = 1, #chars, 1 do
local charat = chars[i]
if not charat.IsDead and string.match(string.lower(charat.SpeciesName), "husk") and not charat IsRemotelyControlled then
table.insert(suitablechars, charat)
end
end
if #suitablechars >= 1 then
Player.SetClientCharacter(client, suitablechars[Random.Range(1, #suitablechars)])
end
return true -- hide message
end
end)
```