So I’m making a multiplayer game in c#. It’s turn based.
The only tutorials I’ve found on networking show how to connect two computers that are on the same network. How can I make a game that I can play with someone while they are in an entirely separate location, or even another state?
I’ve never done anything internet-related in any programming language, so explain like I’m five please.
P2P and dedicated server are the main paradigms for multiplayer games. How complex is the game planning to be and is it going to be commercial?
Unity might be your best bet if you want to go csharp for game building and supports a miriad of methods for this along with guides both written and
YouTube. You may be better off going there. Lesson time:
P2P is straight forward similar to internal network but points to an internet ip instead of internal. The target ip usually is a self declared host with you connecting as a client. The issues involved with P2P usually are port forwarding related due to firewalls for incoming traffic on the host. Lookup upnp to help mitigate that, which will allow more recent routers to auto forward to the requesting host without manual forwards.
Dedicated server would have all players connect to it and mitigate constant requests for updates while temporarily storing game data for the clients as a middleman. This is much more complex design wise but considered much more user friendly networking wise.
Matchmaking would be the next big feature to consider which usually uses a dedicated server regardless, but can be just a broker for connection info for P2P.
To add to this, Unity has a free package called Mirror. It’s essentially setting up a host and client connection with lots of methods available in the package. I’ve been taking a udemy course about the topic now. I’m pretty new as well and to be honest it’s definitely confusing. A lot of communication needs to happen between the server and client(s) and it’s hard to know when that needs to happen in which scripts. Hoping to get better with it! Hope this helps!
This is an oversimplification, but I hope I can manage to convey the technical ideas.
When two computers connect, it is a matter of knowing where to connect to. The information needed to do that is an IP Address AAA.BBB.CCC.DDD and a Port.
In a local network, this is straighforward, and is called an end-to-end connection.
Things get complicated when the internet is involved. The standard IPv4 4-octet AAA.BBB.CCC.DDD standard quickly ran out of addresses for every single possible device, so it is not possible with IPv4 for every device to be “directly” connected to each other. Instead a scheme called Netwotk Address Translation (NAT) was devised, that setup an external facing device (e.g. router) with an internet address, while internal devices along with the routwr would have their own network and corresponding IP Addresses.
This allows outbound connections to work, but not inbound. In other words, you can browse sites (because they have public IP addresses) but you can’t connect to a friend’s LAN hosted game using their computer’s IP Address.
If both of you have a public IP address and port-forwarding is setup on your routers it may be possible to create an end-to-end connection, but public IP addresses tend to cost a bit.
The workaround as others have mentioned is to setup a server that is on the internet with a public IP adddress to relay messages between you and your friend. This is basically how online games work.
You both make outbound connections, so there is no need to workaround NAT. Once the connection is established, it is kept open and the server continually sends messages back along the open connection.
This is what companies that host e.g. Minecraft servers basically do. They buy a range of IP addresses, map them to subdomains, and assign them to virtual servers that run the game server that then listen on a port for incoming connections.
There are services that let you “rent” a server and IP address, such as Azure, AWS, and a lot more, or ones where you just host a specific program, not the entire server.
You can write a server app that continually listens on a port, and run it and the client app (your game) that connects to the server.
Since it’s turn based you don’t have to worry about synchronizing the state of the two clients connected (or you may need to but it is less of an issue)
You will either have to write a sever component that can be hosted in a cloud provider (costs money potentially) like AWS or Google. Or you will need to know each others IP addresses so the applications can communicate (free but will require port forwarding on your router).
As other people mentioned, you can use Unity Mirror (if unity is your platform). If you are doing this from scratch (which I wouldnt suggest it for a beginner (unless the game is pretty basic) , you can use SignalR. You will have to code up a server application that you will keep running 24/7 somewhere (your own server or the cloud). Both players will contact this server application and exchange data.
You’re probably gonna need a networking library of some sort. I use Lidgren.Network