Search Unity

  1. Megacity Metro Demo now available. Download now.
    Dismiss Notice
  2. Unity support for visionOS is now available. Learn more in our blog post.
    Dismiss Notice

Need help understanding new networking methods

Discussion in 'Multiplayer' started by Davdoc, Apr 24, 2017.

  1. Davdoc

    Davdoc

    Joined:
    Apr 6, 2015
    Posts:
    14
    I am trying to program a simple 2D example where a separate server and client interact. I want to program it so that the server is authoritative and controls the player's movement. I know this can be achieved by sending client inputs to the server, and the server updating the client's position. My issue is that all the tutorials are either outdated (using Network View), or far too complicated (I am finding mostly source code). My main confusion is literally in the [Command], [clientRPC], etc... I can set up the basic server and client.

    Could someone please provide or explain a very simple script where the player sends some input, the server handles it and sends back the position?
     
  2. donnysobonny

    donnysobonny

    Joined:
    Jan 24, 2013
    Posts:
    220
    There are potentially two ways that you could do this. The easier way, and the less-efficient way (costs a little more in bandwidth, ram and cpu) would be to use Commands, RPCs or SyncVars. To explain a little bit about how these features work:
    • A SyncVar is a property/variable of an object (particularly one that inherits from NetworkedBehaviour) that is automatically synchronized over the network for you. Really, you should use this for state-changes, or variables that don't change very often. Otherwise you can end up with quite a bit of overhead.
    • A Command is a request made from the client to the server, on a networked object. You start by invoking the Command method from the client-side object, which results in the same method being called on the same object on the server-side.
    • An RPC is a remote call made from the server to all clients on a networked object. You start by invoking the RPC method from the server-side, which results in the same method being called on the same object in all clients.
    You can read up more on these features here https://docs.unity3d.com/Manual/UNetStateSync.html and here https://docs.unity3d.com/Manual/UNetActions.html

    The more-efficient way to do it would be to avoid the use of syncvars, commands and rpcs. Although these features can be very useful, and very powerful, they cost quite a bit in overhead when used frequently, particularly due to their use of class reflection (which is costly on the CPU). Using custom messages allows you to avoid this overhead. The basic idea is that you send serialized objects over the network (classes converted to byte arrays, and back again on the other end) via the various Send* methods. Have a look here for a basic example of how you go about using messages: https://docs.unity3d.com/Manual/UNetMessages.html. This is where things can get a little complicated, and ultimately why the command/syncvar/rpc features exist. Just remember that they are less efficient than messages.

    If you're brave enough to venture into using messages, we can take things a step further, and make things even more efficient by avoiding the serialization step (converting objects to byte arrays and back again) by using NetworkWriters and NetworkReaders. This is definitely a more advanced way to do all of this so i'll not go into a huge amount of detail here, but if you are interested, take a look at the NetworkClient.SendWriter method for an example. The basic idea is that instead of sending an instance of a MessageBase object, you send the raw NetworkWriter instance (which is basically a wrapper around a byte stream). Definitely the most efficient option.

    Hopefully this helps. Good luck!
     
  3. Zullar

    Zullar

    Joined:
    May 21, 2013
    Posts:
    651
    Sending the server a command of the requested position and then waiting for the server to send back the position will add lag due to latency and should only be used for deterministic or turn based games.

    If your character must move smoothly then the Client should own the position and have immediate control over it. For starters you can use [Command] to tell the server where the object was moved to. But have a look at this code for a full network position control script.
    https://bitbucket.org/Unity-Technol...nsform.cs?at=5.3&fileviewer=file-view-default