S&box Wiki

Revision Difference

Networked_Types#543954

<cat>Code.Network</cat> <title>Networked Types</title> Any property on your entity can be networked (synced from server to client). # Making a property networked Adding the `[Net]` attribute makes it networked. ``` [Net] public float HeadSize { get; set; } ``` Classes containing networked members have to be marked as `partial` in order to work properly. This is because codegen adds a supporting class in another file. ## Predicted The properties can also be predicted. ``` [Net, Predicted] public float HeadSize { get; set; } ``` This changes two things * The variable is backed up and restored properly by the prediction system * You can change the value clientside ## Local You can make your networked property Local too, meaning it'll only be sent to the owner of the entity. This is useful if the data is only useful to the controlling client, or for anti-cheat reasons you wouldn't want other players to see the value. ``` [Net, Local] public int Money { get; set; } ``` # Networkable Types | Type Name | Support | |-|-| | float, int, bool, byte, long, short | Any [built in value type](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/built-in-types) will work | | string | Supported | | Enum | Supported | | Vector2, Vector3, Rotation, Angles, Transform | Supported | | Custom Struct | Supported (must contain [only unmanaged types](https://docs.microsoft.com/en-us/dotnet/csharp/language-reference/builtin-types/unmanaged-types)) | | Custom Classes | Must be derived from NetworkClass | | Entity | Supported | | List<float>, List<bool> | Supported - any built in value type | | List<Entity> | Supported - any type of entity | | List<string> | Supported | | Dictionary<x, y> | Planned | ## Custom Structs If you want to transmit a struct it needs to be plain old data. It should contain no strings, no classes, no lists, no entities. ``` public struct AnotherStruct { public int A; public int B; public Vector3 C; } public struct CustomStruct { public float Delta; public AnotherStruct Data; public ulong Dump; } ``` Either of the above are valid ## NetworkClass By deriving a class from NetworkClass you'll be able to sync that using `[Net]` too. It has a few characteristics that you should know about. * You can derive from the class and it'll create the right class on the client. * You can only send variables, entities and structs. No classes, no Lists. * [Predicted] and [Local] attributes won't change anything, these things are dictated by the original [Net]. An example usage of NetworkClass is to provide things like Camera. By networking a Camera class on a var, we allow the server to set the camera class directly and configure its variables. The server can set the `Pawn.Camera` to a `new FirstPersonCamera` and the client will receive that variable and create that class, like magic. This adds a lot of versatility to these kinds of systems. ``` public partial class NetworkClassTest : NetworkClass { [Net] public int IntValue { get; set; } [Net] public Vector3 VectorValue { get; set; } } ``````⤶ ⤶ ## Lists⤶ ⤶ Under the hood the `List` converted to an `ObservableList` and returned as an `IList`. This means you might see some unexpected behaviours. Here's a list.⤶ ⤶ * It will never be null⤶ * Setting it to null will just clear the list⤶ * Setting it to another List will copy from the list⤶ * * Further changes the the original list won't apply to the Networked List⤶ * Any extension methods that expect a List<T> type won't be recognised