đź““

NetworkEntity

public abstract class NetworkEntity : MonoBehaviour
{
    static int currentEntityID = 1;
    public static int GetCurrentEntityID()
    {
        return currentEntityID;
    }
    public static void IncrementCurrentEntityID()
    {
        currentEntityID++;
    }


    public NetworkEntityType type;

    public const int UNASSIGNED_ENTITY_ID = -1;

    public int entityID = UNASSIGNED_ENTITY_ID;

    public STransform sTransform = new STransform();

    //this is to work out inputs for the entity, put it here because
    //this concept could be common to many entities in a big game
    public PlayerInput mostRecentInput;

    //client side stuff for replication
    public bool canBeDeletedIfNotUpdatedByServer = true;
    public bool deleteIfNotUpdatedByServer = false;
    public bool clientCanFakeDestroyMe = false;
    public bool destroyNextFrame = false;

    public PhysicsComponent physicsComponent;
//...

NetworkEntity is the base class all of your game objects need to extend it so they have a chance of being replicated correctly from server to client. Each NetworkEntity is uniquely identified by entityID to ensure data passed across the network is passed to the correct NetworkEntity.

NetworkEntity’s have STransforms to manage their X,Y positions in the game world or UI, however using it is optional.

If this NetworkEntity is controlled by a player, we keep track of the most recent input.

There are a number of variables to help manage a basic NetworkEntity here. Its not practical to send information about every Entity all the time to a client, some are too far away to make a meaningful impact to the player, so if on the client side we don’t get an update, should we temporarily Destroy this NetworkEntity until we get a new update again? And if this Entity should be destroyed for good, we can set destroyNextFrame to true on the server to ensure it is replicated on the clients.

For convenience each NetworkEntity has a PhysicsComponent reference, however this can remain null as it may not be relevant for every NetworkEntity in your game. When extending your own player classes with PlayerNetworkEntity, you should override Read/Write packet and UpdateEntity methods as a minimum.

Note: Recommended to review CyborgPlayer on how to setup a Player class

Methods

public static int GetCurrentEntityID()

Should be called from the server side to get the newest unique entityID to assign to a NetworkEntity

public static void IncrementCurrentEntityID()

Should be called from server side to increment the unique entityID used for the next newly created NetworkEntity

public abstract void UpdateEntity()

This is abstract forcing each sub type of NetworkEntity to implement this method. This is likely to be quite unique for each type of NetworkEntity in your game and this function is likely where their unique characterisation will come across

public virtual void WriteToPacket(NetDataWriter writer)

This method writes data about a NetworkEntity to a packet so it can be sent across a network from server simulation to client. For example you may want to send players XY positions, velocity, HP and other useful information across the network.

NOTE: entityID and type are the ones you must send with each NetworkEntity so that the data is read correctly for each of the clients. Therefore its important that if you override this function in sub types (you likely will e.g PlayerNetworkEntity so you can other data along too) that you call to the super version of this method at the START of this method for each sub type of NetworkEntity:

base.WriteToPacket(writer);

NetDataWriter is a LiteNetLib class, so to see what options you have here reference their documentation.

public virtual void ReadFromPacket(NetDataReader reader)

The method reads data from a packet on the client side from the server. This should mirror the data you write in the previous method excluding entityID and type.

NOTE: ID and Type are consumed first by the NetworkEntityManager to work out which NetworkEntity to pass the rest of the data to, so make sure you DO NOT try reading for entityID and type here. For example if you have a Player NetworkEntity that writes to packet entityID, type, XY pos, velocity and HP, then your ReadFromPacket method should be reading XY pos, velocity, and HP (not ID and type first).

NetDataReader is a LiteNetLib class, so to see what options you have here reference their documentation.

public virtual void DoInput(PlayerInput input)

Currently this method just stores the most recent PlayerInput, however on player controlled NetworkEntities its recommended to put your logic here for controlling your entity through inputs. To do that, just override this method in subclasses where its relevant. See PlayerNetworkEntity for reference.