Search Unity

Custom Server-Client

Discussion in 'Multiplayer' started by georgemore00, May 5, 2021.

  1. georgemore00

    georgemore00

    Joined:
    Mar 26, 2021
    Posts:
    2
    So I am working on a school project where we are making a game in unity and its supposed to be multiplayer. I am not allowed to use any inbuilt server/client libraries like UNET or MIRROR because its a networking course not game dev.

    So far I've got a working UDP .NET server that can receive and send data. Whenever a client connects it stores the info (IPEndPoint) so that it can be used in the future. Whenever the server receives a packet from a client, the server sends the packet to every other client.

    Thank you for reading this far. Should the Server tell the clients which ClientID they have? I guess the packet has to be marked with some kind of ID to identify which client sent it. Also in Unity, How would i go about spawning and "moving other clients gameobjects" when i recieve data that they have changed position. Ive seen that the NetworkManager and UNET uses IsLocalPlayer to identify if its the local player, i cant find the underlying code but i guess its just a bool you set when instantiating the object?

    So what i've got right now is a game, the client can connect to server, the client can send its transform.position to the server, the server prints it out and sends it to other clients.

    All help and ideas is appreciated, and if this is a stupid question please tell me where to read more because all tutorial i see use network libraries and thats what i would do if i was allowed to.
     
  2. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    Sounds like fun.

    What is the purpose of sending every packet from every client, to every other client? Is your server acting only as a relay, where it doesn't actually participate in the game simulation itself? Even in cases where you use a relay server (which is usually so a client can act as the server/host of a game while itself behind a firewall which would otherwise prevent it from doing so), the clients will all send messages through the relay server to the client acting as the host of the game, and the host of the game sends messages to the other clients only if those other clients will need them.

    A lot of this really depends on how you want your game to work. As I already mentioned, Unet doesn't forward client packets to all other clients automatically. Instead the server/host processes client packets, and if they other clients now need new information based on that, then the server/host sends messages to the other clients. The other clients see those messages as coming from the server instead of the original client which kicked the chain of events off. Though if you need to, you could include information from the server to the clients as to which client is responsible for sending the original message.

    Here's a simple example. Lets say we have a Unet game, or other similar network API. The player in front of the keyboard of ClientA presses a key to open a door. ClientA sends a message to the server to indicate it is requesting the door to be opened. The server receives the request, and validates that the character for ClientA is within range to open the door. The server then activates a door opening animation locally, and sends a message to all clients (including ClientA) to activate the same door opening animation (In Unet this might be accomplished using a ClientRPC on a script attached to the door object itself, but there are other ways to do it).

    The door open animation message sent by the server to all clients is not the same message to request the door to be opened as originally sent by ClientA to the server.

    As far as a ClientID is concerned, in this example none of the other clients really need to know that ClientA opened the door. The other clients just need to know that a specific door needs to open. Tracking the specific door in the case of Unet is done using the NetworkIdentity component attached to each networked GameObject.

    It is not a stupid question at all. This stuff is all very complicated and difficult to understand. It is also difficult to explain without assuming a certain level of knowledge already.
     
    Recon03 likes this.
  3. georgemore00

    georgemore00

    Joined:
    Mar 26, 2021
    Posts:
    2
    Thanks for answering Joe, yeah i'd like my server to be non participating in the game itself only forwarding packets between the clients. As of right now, client connects to server by sending message, server figures out if its a new client, if so send back a clientID that the clients recives, clientID will be used to identify original sender of packets as you described.

    I'm still confused about the code structure itself, ive got a Client GameObject and a client script attached to it. A player prefab with playerid, hp, controllers, shooting etc.
    Should my client class be the one instatiating my local playerprefab and other players and store them in a list?

    I thought a GameManager would be a good class for spawning and moving the other players i just dont know how to store them and identify them. I tried having the Client class spawn the GameManager and the GameManager instantiates all players including myself and it worked perfectly but i just think the structure sucks because i dont know if the GameManager should call Client methods when sending localplayer inputs to the server.
     
  4. Recon03

    Recon03

    Joined:
    Aug 5, 2013
    Posts:
    845
    Give TNET a look then.... I have for a few games, used TNET, where I used the server for people to connect to.... Then sent packets between clients. The dev himself uses in his own game... Also the server software don't take any resources you can run on a potato... I have used my Qnap to do it .. Many use a raspberry pi...


    Edit, NM, you said you can't use any... I advise to look at it then, to rip it apart to understand how it works.... it will help you build your own then.

    I been a game developer nearly 3 decades but I never done a ton of net coding, and I will say, that is what I did, I would dig through many of them made, to understand how they worked... Then I was able to use Unreal networking easily and many others...so this had helped me understand networking/net coding, server etc ....and how they worked...

    . So this should help you also build your own.