Search Unity

  1. Welcome to the Unity Forums! Please take the time to read our Code of Conduct to familiarize yourself with the forum rules and how to post constructively.
  2. We’re making changes to the Unity Runtime Fee pricing policy that we announced on September 12th. Access our latest thread for more information!
    Dismiss Notice
  3. Dismiss Notice

Can I make a game using P2P multiplayer?

Discussion in 'Multiplayer' started by SebGM2019, Dec 25, 2019.

  1. SebGM2019

    SebGM2019

    Joined:
    Aug 3, 2018
    Posts:
    34
    I’m planning on making an online game. I’ve used Unity (with Photon’s PUN2), but after some research, I had the idea of instead of using a server, with ccu limit and charge fees, i could make a P2P multiplayer game, so that way, the server would be one of the two players, without having any cost on me. I don’t know If this is possible, and indeed, a good idea. I did a research on this but I couldn’t find a clear explanation or tutorial. PS: It’s not a big game (2d game) think of it as the size of an online version of PONG, where two players fight until one wins, it could have or not a time limit.
     
  2. Antypodish

    Antypodish

    Joined:
    Apr 29, 2014
    Posts:
    10,489
    If you asking question like "Can I make a game using P2P multiplayer?" Means you are not ready yet. But in short, why you think, it is not possible?
     
  3. SebGM2019

    SebGM2019

    Joined:
    Aug 3, 2018
    Posts:
    34
    Because I haven’t found any tutorial or library targeting or mentioning something about P2P. So I don’t know how this could be implemented. Just found a couple of samples in YouTube but none of them provided info of how they were made
     
  4. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    P2P is more or less the default for unity. Unless you go out of your way, if you use UNet or Mirror (can't speak for newer Unity Networking), you will be using P2P by default. It's all pretty standard stuff really, it tends to be easier to set up a networked game to use P2P than dedicated server, since you don't need to worry about a separate server component and paying for physical servers.

    The one issue you're going to run in to though is that most player hosts will not actually be connectable because they are behind a router. The old school solution to this was to have your players manually set up port-forwarding on their routers (booo). Luckily in more modern times you can often forward ports automatically using a bit of code that communicates with the router. Unfortunately not all routers support automatic port-forwarding (UPnP or NATPnP) and even on routers that do support it, there is no guarantee that it is actually enabled. Also some genius invented NAT Punchthrough which can get through some routers without the need for port forwarding, but it doesn't work on all routers.

    So the most modern solution is to use relays that are sort of a middle-ground between dedicated server and peer-hosted. With a relay, no client needs to be able to accept incoming connections (which is what routers tend to block). Instead the initial connection is made to the relay, so no client needs to be able to receive a connection. The relays don't actually host the game though, they just pass packets between clients, one of the clients still acts as the "Host" for all intents and purposes.

    So with all that being said. May I present my own solution: Noble Connect

    With Noble Connect, players are able to act as hosts, always. Whenever possible clients will connect directly to the host using a normal direct connection when permitted, or punching through when necessary and possible. When all else fails a relay is used. Of course relays require a hosting solution, so there is some cost, but it is much less than dedicated servers.

    Thank you for attending my TED Talk.
     
  5. SebGM2019

    SebGM2019

    Joined:
    Aug 3, 2018
    Posts:
    34
    I thought of the multiplayer system something like this: One player is not the server until both connect, and when the match ends, then they disconnect. Like if it was a temporal server. One player tries to play an online match, then, the game looks for another player which is also looking for playing a match, and connects those two players into a single match, one of them is the server, would that be possible?
     
    JanTuts likes this.
  6. thegreatzebadiah

    thegreatzebadiah

    Joined:
    Nov 22, 2012
    Posts:
    836
    You're kind of describing a way of matchmaking now, but I don't know of any existing solutions that work like that. You'll need some sort of external server to handle grouping players together and telling one of them to be the host, but that server wouldn't act as the actual game host, just as a sort of hub to get players together.

    My Match Up plugin is..similar, but doesn't really work the way you want, it requires a player to decide to host first and then allows other players to get a list of hosts and select one to connect to. If you have experience with c++ you could probably use the Match Up server as a base and modify it to achieve your goals. Either way though, you're looking at writing some custom code that is hosted on externally accessible server to handle that part. You could even conceivably do it in Unity with UNet or Mirror, but I wouldn't really recommend it because of all the extra bloat, which will lead to increased server costs.

    Steam lobbies could also get you close I think, using the steamworks api, any player can create a lobby, or get a list of lobbies and join one, then the lobby owner could select a player randomly to be the actual host and everyone could join them. In that case the external server that handles the lobbies is hosted by steam and you don't need to worry about it or pay for it.

    If you need something that considers all players that are looking to join a match and takes in to account something like rank (think dota or lol) then steam lobbies won't work for you. You need control over the external server code to get behaviour like that.

    The actual hosting part though, where a player becomes a host temporarily when they want to play and then it gets shut down when the game is over is all standard operating procedure, it's just your method of matchmaking that would require some custom work. And there may even be solutions out there that work like that already, I'm just not aware of any.
     
    Last edited: Dec 26, 2019
  7. Joe-Censored

    Joe-Censored

    Joined:
    Mar 26, 2013
    Posts:
    11,847
    If you want to do match making or otherwise connect players without them editing router settings and directly sending each other their IP addresses, you need some server to facilitate getting the connection going automagically. There's several ways to do it, but even the simplest are more complicated than a direct IP connection on a LAN, so you don't see this type of thing in beginners tutorials outside of cloud service based networking solutions like Photon, or for pay for assets which specifically offer this functionality.
     
  8. LetmeDwight

    LetmeDwight

    Joined:
    Apr 9, 2020
    Posts:
    125
    Whats with the Peer to Peer Servers from Trailmakers?
    you can only make in multiplayer a server with max. 4 slots and what is running on the host Computer. Because if the host have a potato, the server wil lag or maybe crash. if the host will have a good computer you can play normaly so I think there is no UNET F***tion inside of it... SO how can I make p2p servers in my game that works like the Trailmakers servers?
     
  9. LegendarX

    LegendarX

    Joined:
    Oct 9, 2021
    Posts:
    1
    Can I write a p2p server for 4 players (2D)? Are there any examples?
     
  10. Baklusha

    Baklusha

    Joined:
    Dec 18, 2018
    Posts:
    7
    p2p is a way how computers can be connected in a network. When we say p2p we mean there is no server like in a server-client mode but the responsibility is shared among clients (peers). That is why it is not clear what you mean by "p2p server".
    Do you mean the server which can handle 4 connections and be a client at the same time?

    In any case the answer to your question is "yes" but the solutions will be different depending on the context.
     
    Akaton97 likes this.