Get docs in line and added networking manual

This commit is contained in:
EvilFactory
2022-10-29 19:44:23 -03:00
parent 09979cb4c9
commit b77dde156b
5 changed files with 93 additions and 14 deletions

View File

@@ -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

View File

@@ -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

View File

@@ -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
```