Skip to content

Lua (Incomplete)

Evil Factory edited this page Sep 3, 2021 · 53 revisions

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

(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.
  • characterCreated - Gets called every time a character is created, provides a Character as argument.
  • characterDeath - Gets called every time a character dies, provides a Character and the killing affliction 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.
  • afflictionUpdate - Gets called every time an affliction updates, provides the Affliction, CharacterHealth, Limb (can also be nil) and the deltaTime.
  • itemUse - Gets called every time an Item gets "Used", provides the Item,the character using the item, and the targetLimb. You can return true to cancel the usage.
  • itemSecondaryUse - Same as itemUse, but theres no targetLimb.
  • itemApplyTreatment - Gets called whenever an item is used as a treatment (eg. bandages); Provides the Item,the using Character,and the Character getting treated, and the Limb getting treated.
  • itemDrop - Gets called whenever an item is dropped, returns the Item, the Character that dropped the item. You can return true to cancel.
  • itemEquip - Gets called whenever an item is equipped, returns the Item,(Character) user. Return true to cancel.
  • itemUnequip - Same as itemUnequip, but for unequipping.
  • changeFallDamage
  • gapOxygenUpdate - Gets called every update when a gap passes oxygen, returns the Gap,First Hull and the Second Hull.
  • signalReceived - Gets called everytime an Item receives a wire signal, providing: Signal,Connection
  • wifiSignalTransmitted - Gets called everytime a WifiComponent starts transmitting a signal; Provides the WifiComponent,the transmitting Signal, and sentFromChat. You can return true to cancel the signal from being transmitted.
  • serverLog - Gets called everytime something is logged to the Server Log, returns the text, and the ServerLogMessageType

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

  • bool RoundStarted
  • 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)
  • ContentPackage[] GetEnabledContentPackages()

Random

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

Timer

  • float GetTime()

Vectors

Networking

Client

Character

Item

Level

Submarine

WayPoint

AfflictionPrefab

Clone this wiki locally