diff --git a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs index e83cb595c..52d556c2e 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/Items/Item.cs @@ -3011,7 +3011,7 @@ namespace Barotrauma } } - var result = GameMain.LuaCs.Hook.Call("item.readPropertyChange", this, property, parentObject, allowEditing); + var result = GameMain.LuaCs.Hook.Call("item.readPropertyChange", this, property, parentObject, allowEditing, sender); if (result != null && result.Value) return; diff --git a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs index 7ef33e2ad..122e21723 100644 --- a/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs +++ b/Barotrauma/BarotraumaShared/SharedSource/LuaCs/Lua/LuaClasses/LuaGame.cs @@ -470,11 +470,6 @@ namespace Barotrauma GameMain.Server.SendChatMessage(msg, messageType, sender, character); } - public static void SendMessage(string msg, int messageType, Client sender = null, Character character = null) - { - GameMain.Server.SendChatMessage(msg, (ChatMessageType)messageType, sender, character); - } - public static void SendTraitorMessage(Client client, string msg, Identifier missionid, TraitorMessageType type) { GameMain.Server.SendTraitorMessage(client, msg, missionid, type); @@ -482,12 +477,9 @@ namespace Barotrauma public static void SendDirectChatMessage(string sendername, string text, Character sender, ChatMessageType messageType = ChatMessageType.Private, Client client = null, string iconStyle = "") { - - ChatMessage cm = ChatMessage.Create(sendername, text, messageType, sender, client); + ChatMessage cm = ChatMessage.Create(sendername, text, messageType, sender); cm.IconStyle = iconStyle; - GameMain.Server.SendDirectChatMessage(cm, client); - } public static void SendDirectChatMessage(ChatMessage chatMessage, Client client) diff --git a/luacs-docs/lua/baseluadocs/Item.lua b/luacs-docs/lua/baseluadocs/Item.lua index 3fa51730b..508340301 100644 --- a/luacs-docs/lua/baseluadocs/Item.lua +++ b/luacs-docs/lua/baseluadocs/Item.lua @@ -10,10 +10,6 @@ Barotrauma source code: [Item.cs](https://github.com/evilfactory/LuaCsForBarotra Item = {} ---- Adds to remove queue, use this instead of Remove, to prevent desync. --- @realm server -function Item.AddToRemoveQueue(item) end - --- Gets a component from an item by a string name. -- @treturn Component component -- @realm server diff --git a/luacs-docs/lua/lua/Hooks.lua b/luacs-docs/lua/lua/Hooks.lua index 9c6f16e4e..5788feb33 100644 --- a/luacs-docs/lua/lua/Hooks.lua +++ b/luacs-docs/lua/lua/Hooks.lua @@ -327,3 +327,15 @@ function human.CPRSuccess(animController) end --- Called after the CPR skill check fails. -- @realm shared function human.CPRFailed(animController) end + +--- +-- @realm client +function keyUpdate() end + +--- Called after a net message is received +-- @realm shared +function netMessageReceived(netMessage, header, client) end + +--- +-- @realm shared +function item.readPropertyChange(item, property, parentObject, allowEditing, client) end \ No newline at end of file diff --git a/luacs-docs/lua/manual/networking.md b/luacs-docs/lua/manual/networking.md new file mode 100644 index 000000000..8e1c0c560 --- /dev/null +++ b/luacs-docs/lua/manual/networking.md @@ -0,0 +1,79 @@ +# Networking + +# Singleplayer +Singleplayer is the easiest of all, as there's no networking, so everything is already synced for you! +In singleplayer the global variable `CLIENT` is set to true and `Game.IsSingleplayer` is also true. + +# Server +The server is responsible for receiving inputs from clients and syncing state with all other clients. +The global variable `SERVER` is set to true if we are a server. +The only difference between a **Dedicated Server** and a **Player-Hosted Server** in Barotrauma is the fact that the latter uses Steam Networking to communicate with clients, if you for some reason want to know if you are running inside a Dedicated Server, you can use `Game.IsDedicated`. + +# Client +The client is the one who connects to servers. The global variable `CLIENT` is set to true and `Game.IsMultiplayer` is also true. + +# Syncing + +## Serializable Properties +Serializable Properties are special members that are able to be synced with clients (and also with the server in some cases), they are very useful for server-side only code. + +Example showing how to sync the sprite color of an item without any client-side code: + +``` +local item = ... + +item.SpriteColor = Color(0, 0, 255, 255) + +local property = item.SerializableProperties[Identifier("SpriteColor")] +Networking.CreateEntityEvent(item, Item.ChangePropertyEventData(property)) +``` + +This is also possible to do with item components: + +``` +local item = ... +local light = item.GetComponentString("LightComponent") + +light.LightColor = Color(0, 0, 255, 255) +local property = light.SerializableProperties[Identifier("LightColor")] +Networking.CreateEntityEvent(item, Item.ChangePropertyEventData(property)) +``` + +## Sending Custom Net Messages + +This is one of the ways you can send data between the client and server. + +Example on sending data from client to server + +``` +if CLIENT then + -- send from client to server + local message = Networking.Start("something") + message.WriteString("hello") + Networking.Send(message) +end + +if SERVER then + -- receive in server + Networking.Receive("something", function(message, client) + print(client.Name .. " sent " .. message.ReadString()) + end) +end +``` + +Example on sending data from server to client + +``` +if CLIENT then + Networking.Receive("something", function(message) + print(message.ReadString()) + end) +end + +if SERVER then + -- send from server to client + local message = Networking.Start("something") + message.WriteString("hello") + Networking.Send(message, Client.ClientList[1].Connection) +end +``` \ No newline at end of file