-1

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.

mrarray
  • 1
  • 1
  • 2
  • Welcome new user. Can you at least state what type of game you're making? ie is it live action (like poing or car racing) or is it more like a puzzle (perhaps chess etc). – Fattie May 05 '21 at 15:34
  • I just spent 6 months in my 40h job on writing our own pure c# based MultiUser backend for local network (and an online mode using Photon) ... how much time does your school give you for that? ^^ – derHugo May 05 '21 at 15:37
  • `Should the Server tell the clients which ClientID they have?` if there is more things happening that is client ID based yes. `I guess the packet has to be marked with some kind of ID to identify which client sent it` not for the server -> it already knows each clients endpoint .. if you need the senderID on the receiving client -> yes send it in the package. For the transforms: you need a high performance (de) serialization and want to pack multiple data snippets into bigger UDP packages since they have overhead (20b UDP Header vs 40b one position + rotation + scale data) – derHugo May 05 '21 at 15:43
  • In general you want 2 channels: Reliable (TCP) for all command based things and Unreliable (UDP) for all continous updates (transform) .. whether all clients know each other (P2P) or the server acts as a relay is up to you .. both have their pros and cons – derHugo May 05 '21 at 15:45
  • heh! I was about to say almost exactly what @derHugo just said. Our second-previous job was **~35 weeks** building a custom MMP system with Unity, so, it is a massive cloud system (basic stack is aws/node/express/sql) and indeed the Unity side. – Fattie May 05 '21 at 15:49
  • The trickiest part is: how do you identify GameObjects (etc) over the network so you know what object different commands are referring to (aka NetworkIdentity)? Here my approach was: clients can request IDs from the host, they get reserved, and then everyone is told to spawn an object and register it with that requested ID .. still .. as said this is not an "In need it tomorrow" task you got there ^^ – derHugo May 05 '21 at 15:50
  • ( indeed the project I mention *did not even* include realtime transform stuffs, as dH mentions. ) – Fattie May 05 '21 at 15:51
  • I guess OPs next question will be "but how do I do predictive positioning inside the frame?" :O – Fattie May 05 '21 at 15:53
  • @Fattie haha we just lerped and removed prediction entirely ^^ it's an industrial solution without a competitive mode ;) .. I'm pretty sure the next question will rather be how to do multithreading in Unity ^^ – derHugo May 05 '21 at 15:57
  • really .. whaa !!! I was going to mention to the OP, if it even matters, it is an INCREDIBLE amount of work to actually implement "sockets" using .Net from scratch. Eg endless questions like, https://stackoverflow.com/questions/63712116/in-unity-net-websocket-solving-sendasync-already-one-outstanding-problem And a number of well-known libraries are dead. We found the only realistic solution currently is the library Marfusios/websocket-client . On the server side (if that's the architecture), I love websockets/ws but it's far from easy. Spent 2 weeks writing solid disconnect detection :/ – Fattie May 05 '21 at 16:02
  • Wow i really do feel stupid reading all the answers. The game is supposed to be a 4 player shooter game, the ingame stuff is already done, the network part is the only thing left. @derHugo as you said the client ID is needed so that other clients know who the sender is. I was thinking off having the clients initialize the connection by sending a simple message "Connected" and then wait for the server to send back which Client ID they have. When the clients know its own ID it can spawn 3 other players with their ID. Their ID will be 1-4 excluded my own client ID. Is this a bad strategy? – mrarray May 05 '21 at 18:09
  • The question is how secure does your solution need to be? This way someone could connect to your server without sending the connect message => this client can listen to all communication without ever appearing in the session ^^ – derHugo May 05 '21 at 19:35
  • Haha I appreciate you for still being here, for this project security is not important. I know its bad but for this time ill let it be. I mean the intruder can connect to my server aslong as he knows my public ip and hostPort, maybe the connect message can be a password otherwise the server wont let that client connect? – mrarray May 05 '21 at 19:44

1 Answers1

-1

You really face a long journey!

Should the Server tell the clients which ClientID they have?

On the server side you should have these abilities:

  1. Send to everyone connected

  2. Send only to some specific connection

If you don't have that sorted out, you'll have trouble.

Should the Server tell the clients which ClientID they have?

It's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

A simple approach is: when a client is launched, just create a UUID and use that. So, in this example the client is choosing their own ID. The first communication to the server is something like "hi, my uuid is2i23u4y2i34u2y4ui and my user's name is Jane Jones". On the server side, keep track of all the uuids and connections and nicknames, etc etc.

(If you're not already familiar with math concepts like UUIDs, you face a long journey.)

As I say, it's your choice. The server can "choose all the IDs" or the clients can "choose their own id".

"How would i go about spawning and moving other clients gameobjects etc

Well in clientA you'd have a function called something like "SpawnARobot(position)"

When commands arrive at clientA, you'll parse the commands. You'll have a big switch statement to know how to handle various commands. One of your commands might be "spawn" and when clientA receives a "spawn" command clientA would call "SpawnARobot(position)"

Here's literally an example of some commands (ie, just some text) arriving, and then the relevant functions/whatever being called:

enter image description here

Note that you have some really basic concepts to work out.

In some games only the server "decides" that something is spawned, or, whatever. The clients are completely dumb and essentially just "display what they are told".

In other games the server doesn't really do anything other than pass around commands, and, the clients themselves "decide" that something should be spawned, or, whatever. (That is often an "rpc" approach.)

Also don't forget there is a HUGE basic confusion between ... say you have three iPhones playing your Game. We speak of a "server" (call it a "master controller") which is one of those three iPhones, so each GameOnAnIPhone has your "client" but one (only) is also the "server" (or "master controller"). In complete contrast to that you can have literally "a server" in the cloud, ie your code (very likely Node.js) running on an AWS instance, and all three GameOnAnIPhones connect to the "server server".

All of these concepts are incredibly different, you have to sort out these basics I'm afraid.

Fattie
  • 27,874
  • 70
  • 431
  • 719
  • I get what you're saying about server vs client authority about objects. My idea is having the server recieving data from clients ex transform position, and then broadcast it to the other clients. Then the other client figures out which client it should move on their local game. Why do i need the client to tell the server that i want to be spawned? The clients can spawn all of the other players locally from the beginning? Its not an advanced game. As of right now, when i start the game the client connects to the server, the server recives data and forwards it to other clients. – mrarray May 05 '21 at 18:13