Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. Dismiss Notice

Resolved How to send grid data?

Discussion in 'Netcode for GameObjects' started by Hellhound_01, Jul 12, 2023.

  1. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    95
    I try to change my grid based build from single player to multiplayer. I've a grid based on Tiles, which are stored in a two dimensional array. I've a GridGenerator which generates those tiles only on server side. Now I've to broadcast this data to all connected clients. What is the better approach using NetworkVariables or ClientRPC?

    I started to test NetworkVariables. My Tile struct is based on INetworkSerializable and IEquatable<Tile>. Using a NetworkVariable<Tile> anything works fine, with an array or list (also tried NativeArray and NativeList> I run in this exception during initialization:

    Any idea what I'm doing wrong? The docs and examples I found only use simple primitive values. Thanks for any help.

    Using Netcode 1.5.1 and Unity 2022.3.3f1

    Best regards,
    Hellhound
     
  2. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
    I've done something similar and NetworkList<Tile> worked fine for me.
     
  3. lavagoatGG

    lavagoatGG

    Joined:
    Apr 16, 2022
    Posts:
    218
    If those tiles don't change much and are mostly static, then clientRPC is a better approach.
     
    Hellhound_01 likes this.
  4. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    95
    This is my actual quite simple Tile implementation. No magic:

    Code (CSharp):
    1. public struct Tile : INetworkSerializable, IEquatable<Tile>
    2.     {
    3.         int posX;
    4.         int posZ;
    5.  
    6.         public Tile(int posX, int posZ)
    7.         {
    8.             this.posX = posX;
    9.             this.posZ = posZ;
    10.         }
    11.  
    12.         public bool Equals(Tile other)
    13.         {
    14.             return (other == this);
    15.         }
    16.  
    17.         public static bool operator ==(Tile left, Tile right)
    18.         {
    19.             return (left.posX == right.posX);
    20.         }
    21.  
    22.         public static bool operator !=(Tile left, Tile right)
    23.         {
    24.             return (left.posX != right.posX);
    25.         }
    26.  
    27.         public void NetworkSerialize<T>(BufferSerializer<T> serializer) where T : IReaderWriter
    28.         {
    29.             serializer.SerializeValue(ref this.posX);
    30.             serializer.SerializeValue(ref this.posZ);
    31.         }
    32.     }
    Always crash on Initialization of my GridGenerator inheriting NetworkBehaviour and own a NetworkObject:
    public NetworkVariable<NativeList<Tile>> grid = new NetworkVariable<NativeList<Tile>>();

    This works:
    public NetworkVariable<Tile> grid = new NetworkVariable<Tile>();

    Tried several things, no result. Running out of ideas.

    @lavagoatGG Most data will be static, but also some Tiles changes every turn (should be equal to turn based tactic lice xcom and jagged alliance).
     
  5. cerestorm

    cerestorm

    Joined:
    Apr 16, 2020
    Posts:
    598
  6. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    95
    @cerestorm Thanks for the hint, I've overread this. Will give the list a try.
     
  7. Hellhound_01

    Hellhound_01

    Joined:
    Mar 5, 2016
    Posts:
    95
    @cerestorm Short feedback: It works with the NetworkList thanks a lot.
     
    RikuTheFuffs and cerestorm like this.