Skip to content

Lua (Incomplete)

Evil Factory edited this page Aug 17, 2021 · 53 revisions

If you want to learn how Lua works and the syntax, you can check this website https://www.tutorialspoint.com/lua/lua_overview.htm

(not everything is documented here, i'm still adding stuff)

Getting started

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 rerun 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.

Global classes

Hook

Hooks are basically functions that get called when events happen in-game, like chat messages.

You can add a new hook like this:

Hook.Add("roundStart", "myRoundStartHook", function()
     print("round has started!")
end)

The first argument is the event name, the second is the hook name and the third is the function that will be called. other example:

Hook.Add("chatMessage", "myChatMessageHook", function(message, client)
     print(client.name .. " typed in chat " .. message)
end)

List of hooks

  • chatMessage - Gets called every time someone sends a message, the function provides message and client as the arguments, if you return true, you will cancel the message.
  • think - Gets called every update in-game.
  • clientConnected - Gets called every time someone joins the server, provides a client as argument.
  • clientDisconnected - Gets called every time someone leaves the server, provides a client as argument.
  • roundStart - Gets called every time the a round starts.
  • roundEnd - Gets called every time the ends.
  • characterDeath - Gets called every time a character dies, provides a Character as argument.
  • afflictionApplied - Gets called every time an affliction is applied, has 3 arguments, CharacterHealth, Affliction, and Limb (can be nil), you can cancel the affliction by returning true.
  • itemUse
  • itemSecondaryUse
  • itemApplyTreatment
  • itemDrop
  • itemEquip
  • itemUnequip
  • changeFallDamage
  • gapOxygenUpdate
  • signalReceived

Player

  • Character[] GetAllCharacters()
  • Client[] GetAllClients()
  • SetClientCharacter(Client client, Character character)
  • Kick(Client client, string reason = "")
  • Ban(Client client, string reason = "", bool range = false, float seconds = -1)
  • UnbanPlayer(string player, string endpoint)

Game

  • SendMessage(string msg, ChatMessageType messageType, Client sender = null, Character character = null)
  • SendTraitorMessage(Client client, string msg, string missionid, TraitorMessageType type)
  • SendDirectChatMessage(string sendername, string text, Character sender, ChatMessageType messageType = ChatMessageType.Private, Client client = null, string iconStyle = "")
  • Explode(Vector2 pos, float range = 100, float force = 30, float damage = 30, float structureDamage = 30, float itemDamage = 30, float empStrength = 0, float ballastFloraStrength = 0)
  • SpawnItem(string name, Vector2 pos, bool inventory = false, Character character = null)
  • Log(string message, ServerLogMessageType type)
  • Character Spawn(string name, Vector2 worldPos)
  • ExecuteCommand(string command)
  • OverrideTraitors(bool o)
  • OverrideRespawnSub(bool o)
  • AllowWifiChat(bool o)
  • DispatchRespawnSub()
  • RemoveItem(Item item)
  • Signal CreateSignal(string value, int stepsTaken = 1, Character sender = null, Item source = null, float power = 0, float strength = 1)
  • ItemPrefab GetItemPrefab(string itemNameOrId)
  • AddItemPrefabToSpawnQueue(ItemPrefab itemPrefab, Vector2 position, DynValue spawned = null)
  • AddItemPrefabToSpawnQueue(ItemPrefab itemPrefab, Inventory inventory, DynValue spawned = null)

Random

  • int Range(min, max)
  • float RangeFloat(min, max)

Time

  • float GetTime()

Vectors

  • Vector2 CreateVector2(x, y)
  • Vector3 CreateVector3(x, y, z)
  • Vector4 CreateVector4(x, y, z, w)

Client

Character

Item

Level

Submarine

WayPoint

AfflictionPrefab

Examples

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)
local characters = Player.GetAllCharacters()
local biteWoundsPrefab

for k, v in pairs(AfflictionPrefab.ListArray) do
    if v.name == "Bite wounds" then 
       biteWoundsPrefab = v 
       return 
    end
end

for k, v in pairs(characters) do
   v.CharacterHealth.ApplyAffliction(v.AnimController.MainLimb, biteWoundsPrefab.Instantiate(100));
end
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)
-- 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)

Clone this wiki locally