Search Unity

Networking Tutorial

Discussion in 'Connected Games' started by DethRaid, May 19, 2012.

  1. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    I'm creating an RTS game which needs multiplayer functionality. The basic network structure I'm pursuing is a client-server one with a non-authoritative server, as said server will run on the machine of the player who creates the game. Despite the resources available from Unity, I just don't really know where to begin. Does anyone know of a tutorial on networking in Unity, preferably in text form?
     
    Last edited: May 21, 2012
  2. seon

    seon

    Joined:
    Jan 10, 2007
    Posts:
    1,441
    I'd highly recommend getting this:

    http://u3d.as/content/m2h/ultimate-networking-project/1ut

    The guide itself is great if you are wanting to get up to speed in Unity networking quickly, and the tutorials and examples are great and offer code you can *almost* just drop into your game.

    Also keep in mind that Unity networking isn't real P2P networking. It's client hosted p2p, meaning that all traffic goes via the host, even if it is not intended for the host. The host acts like a "server" for all traffic. This puts a lot of strain on the host as they take a massive hit for all traffic.

    Also, remember that using Unity's networking will dramatically limit those that can host games as it requires NAT traversal that only works on well setup home networks, and often gets blocked by firewalls, corporate networks and of course, cant run on 3G/4G if you are looking for offering the game on iOS.Android devices.

    I would really recommend looking at Photon instead. With the PUN (Photon Unity Networking plugin), the syntax is almost identical, and Exit Games now offer a super cheap photon cloud service to host your game:

    http://u3d.as/content/exit-games/photon-unity-networking-free/2ey

    It's what I use for Cubemen and I have had 0 networking issues for player connectivity issues.
     
  3. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    There are two problems with both of those ideas: They cost money, and they're not my own code.

    I'm creating this game for a school project. I don't have any reason to think that I'll get any revenue from it. Spending $85 on a tutorial that three out of five people say isn't worth the money is not very high on my list of things to do right now. Although the PUN system looks good, it says that it's free for the first month, but that suggests that there'll be a monthly payment. Paying monthly for a service I'd use once in a free game that I'm making for a school project doesn't make much sense to me either.

    What I'd like is a tutorial that tells me how to create a networking system in Unity. I want to look at it, read through it, and know how to write code to emulate it. I'm not interested in any sort of plugin, nor any other collection of scripts one might use, nor anything that costs money for the reasons enumerated above. Does anyone know of any such tutorial?
     
  4. tobiass

    tobiass

    Joined:
    Apr 7, 2009
    Posts:
    2,174
    What's the level of interest you are going for?
    Networking? Low level architecture of using sockets, packaging and sending data, reliable versus unreliable sending, etc?
    Making a multiplayer game happen? Sending positions, shots, start and end of rounds? Game logic?

    The first one is a general topic and not Unity specific. You should look out for other networking solutions and how they treat the actual traffic. Lidgren is a common suggestion. It's C# and you could learn a lot. There are few, general articles for this topic outside of the Unity world. Try stackoverflow to find some.

    The second is Unity specific. You can choose from a lot of options in this area. Unity's networking is fine for a many projects (including "free for school") and the only way to get a webplayer to host games. It lets you concentrate on the game-side and there are guides to find in the forum and in the Unity documentation. Please look around yourself ;)

    All external solutions need a server running on some machine, so you will need to learn some administration aside from just getting your game going. It's a bit more complicated than just using the internal Unity solution.
     
  5. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    This is not a peer-to-peer architecture, it's a client <-> server one.

    No, because it doesn't exist pretty much. Networking in games is a very badly documented area to be honest, very few books or other material exist. There are random, more or less, famous articles all over the internet (1500 archers, gafferongames, source engine networking).

    Also, you say you want to do networking for an RTS game, it's something which is very hard to do in Unity due to the fact that nothing inside of unity is deterministic, and RTS networking builds on a lock-step model which requires the simulation of all peers to run in sync and always produce the same output for a specific input.

    Also, if you don't wanna spend 85$ on a tutorial that is generally considered good for beginners, then I'm not sure how we could help you. The world doesn't owe you a free tutorial, and no good one exists. Most people in the games industry (even hobbyists) are used to paying for stuff as it's probably the largest area of software development which lacks a very big open source/free community due to the amount of money you can make here if you produce a successful game.
     
  6. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
  7. Apex5

    Apex5

    Joined:
    Mar 23, 2012
    Posts:
    19
    Coincidentally I started an RTS a couple of days ago using the CG cookie tutorial; which I think does a really good job of explaining it and actually works with the current version of Unity. I'm doing the same as you with the non-authoritative server but my games probably a bit different from yours, this is what I've got so far: http://www.youtube.com/watch?v=RtfsJ66P3QU&feature=youtu.be
    Let me know if you're running into any problems, I've had quite a few moments of banging my head against a wall :p
     
  8. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    Building a networked RTS without lock-step? I'm sorry, but what?
     
  9. Apex5

    Apex5

    Joined:
    Mar 23, 2012
    Posts:
    19
    I'm not entirely sure what you mean by that, I presume you're talking about: http://en.wikipedia.org/wiki/Lockstep_(computing) which is essentially a authoritative server? I'm just building mine for fun so I'm not really too worried about people hacking the client (since all my friends are non-programmers :p)
     
  10. AdrianC

    AdrianC

    Joined:
    Apr 22, 2012
    Posts:
    150
    He's talking about this: http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php

    Lockstep is pretty much the only reasonable way to handle large amounts of units over a network. Kind of a pain to implement though, I've been working on this for a few days, and I can't seem to keep the clients in sync (they are ok at first but eventually problems appear) so if anybody has any tips I would really appreciate it.
     
  11. Apex5

    Apex5

    Joined:
    Mar 23, 2012
    Posts:
    19
    I think I (partly) understand now :p
    The way I'm dealing with the large amount of units is by sending only the movement target position by RPC when a player initiates a order, all the legwork is done by the same algorithms (a really basic clientside prediction) and catchup using Time.delta for slower computers.
    I can see why this is bad as the clients aren't properly synched due to the latency delays (I'm not using the turn based structure) and that anyone who alters the game client can change the algorithms, give themselves super speedy units etc.

    If my thinking's correct on this: the host is able to synchronize all of the clients by defining how long a frame should last ahead of time by analysing the "round trip ping" so each client will be running the exact same game loops at the exact same time?
    It can see why that would be helpful with international players/large latency.

    Thanks for the link, I can see myself coming back to that alot :D
     
    Last edited: May 31, 2012
  12. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    Apex, I'm handling synchronization much the same way you are. Whenever the player gives an order to a unit, such as to move to a certain place, an RPC function is called on every instance of that unit across the network. That, in my opinion, uses the minimal bandwidth while maintaining synchronization. I'm having trouble getting the server to set up the game properly, so it's as yet untested.

    To prevent cheating, whenever a building or unit is created or destroyed, an RPC is called on every game instance. That RPC checks to make sure that the desired action can happen. For instance, if I were to build a building, an RPC to make sure that I have enough resources on every client across the network. If every client says that yes, I do have enough resources, there's no problem. If one client is different, it is corrected and whichever player is connected to that client is given a message telling them so. If half of the clients say that I do have enough resources and half say that I don't (there are a maximum of 4 players at a given time), then all clients are set to whatever gives the least advantage for whoever tried to do an action and I know I need better synchronization. I got the idea from the March issue of Game Developer magazine.
     
  13. Apex5

    Apex5

    Joined:
    Mar 23, 2012
    Posts:
    19
    Definitely seems like a good idea, I wasn't sure about using due to the extra traffic but I guess it's not really that much considering. If the client the server is hosted with is meant to be the exact copy of each other client I presume you could just check against only the server; though I do remember reading something about the majority vote some when, so that's probably the better idea. It's either that or check a have a large ruleset and compare the client's against the server's whenever they connect, that would probably make linking everything together a nightmare though. If you end up posting a vid/screenies let me know, I'd be interested to see how it turns out :D

    I think it might be worth implementing the Aoe turn based sort of system whilst remaking it mine scratch to make it fully authoritative; gaah :p
     
  14. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    I just wanted to inform you that the way you're doing synchronization wont work in a complex game, and is prone to hacking (one client can deny all requests for someone to build, etc.)

    Also the movement sync doesn't work out in the end either, not that I think it matters for you two now ... but in the end, for a real game, it would never work.

    A lock step simulation with a server is the only way to build a networked RTS games with many units.
     
  15. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    Well, I'm going to try doing it this way anyways. If it doesn't work, it doesn't work, but I'll still try.

    If one client denied requests to build but the other three said that it was fine, the build would happen. If three out of four clients agree on a thing, it is assumed that the thing is good. The dissenting client will initially be assumed to just be out of sync, but if it gives a different outcome five or more times then it will be labelled as a cheater and its vote will be ignored. Yes, two players could hack, but again, that would just set an outcome to whatever gives the least advantage to whoever requested it. Yes, that could cause a problem, but I'm creating this game for a school project primarily to learn more about Unity, so cheating protection isn't all that important at the moment. I'm going to have six months to polish this game, assuming I get the multiplayer working within two weeks, so I can look into more advanced cheating protection during that time.

    Responding to your second point, I have two things. First, I fully intent to make a "real" game. If by "real" you mean something on the scale of Starcraft II, with a following of equal size, then you are right that that is not my goal. Problem is, though, games don't have to be popular to be real. Mostly they just have to exist.

    Second, could you please explain more in-depth how my movement sync wouldn't work at all? Not trying to be rude here, I'm genuinely curious. Thanks.
     
  16. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    It will de-sync, units will appear at slightly different positions on different computers, in the beginning it might be nothing, but in the end it will snowball out of control.

    Also, if you intend to be able to handle the amount of units say StarCraft2 can have in one game, then you will end up getting insane spikes during battles or times of heavy movement, which will cause your simulation to lag on at least some peers.
     
  17. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    Also, if you intend to have all clients verify all actions, then I assume this will be for things like "unit A damaged unit B" also, which will cripple your bandwidth, and lag, and well, everything.

    I don't even understand why you're doing the whole "clients verify everything", it gives you nothing over using a dedicated server.
     
  18. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    First of all, I'm not verifying every little thing. I'm verifying when a unit is created or destroyed, or when a building is created or destroyed. Yes, this will lag during battles, but how much? I believe that I can design a system where such little information is sent that this isn't a problem, but we shall see. I'm not verifying everything, though.

    I'm inclined to think it won't. Whenever a player moves his units, an RPC will be called on every client - moving said player's units to the exact same place. Every time a move happens, the exact same inputs are given to every client. Given that they all use the same movement algorithms, I can find no reason why every client wouldn't produce the same outputs.

    Aha, that's the problem, isn't it? Dedicated servers. Or, rather, that I don't have any. I could use one of my home computers as a server, However, this would mean that that computer couldn't be used for anything else. I couldn't use my laptop, as I need to use that for other things. I couldn't use my desktop, as my sister uses that for homework and I'll loose access to that in about a year when I go off to college. I couldn't use my dad's work computer because he needs to use that. I couldn't use my parent's desktop, as they need to use that, I'll lose access to it when I go off to college, and it has very little processing power. So, what could I do? I could buy a server. That costs money, though, and currently all my funds are directed towards getting Unity Pro. As you may have guessed, I am in high school. I work part-time for minimum wage. Am I going to devote hundreds of dollars, nine months' worth of work when school is in session, just to buy a server so I can test my school project using a "real game" way of doing things? Nope. I could rent a server. That, again, costs money. While I wouldn't have to know how to use it, I would still have to know how to actually make a server, or at least set one up, and as my dad would say, that's pretty high up on the list of things that aren't going to happen. So, no server. No machine dedicated to running game code. What then?

    As previously mentioned, I read an article in the March issue of Game Developer magazine detailing how one could create a (mostly) cheat-resistant peer-to-peer network. If at all possible, I highly recommend you read that article. Although that's not quite possible with what I know of Unity networking, I can come close. The flow of the system is this: First, a player creates a game. They get to be the server, yay! In my architecture, this means very little. Then, other players connect. They choose their race, choose a color, and then whoever's running the server starts the game. RPC calls are used to initialize every game to the same state (this is the bit I'm still working on). Then it's game time. Player A creates a unit, or at least he tries to. An RPC is sent out which checks if he has enough resources. If the Player A script on Player B's client says he doesn't, but everyone else says he does, Player B is given a message saying that his client is out of sync and has been changed accordingly (and, of course, it is changed accordingly). If everyone except Player A's client says that he doesn't have enough resources to build the unit he wants to, the Player A's client is corrected. The messages saying weather or not Player A has enough resources are sent to the server, which evaluated which ones are different and send out the correct RPCs. No dedicated server needed.

    Another reason why dedicated servers are bad: scalability. If my game takes off and everyone is playing it all at once (unlikely, but work with me here), I'll be in a world of hurt. Were I to have a dedicated server, I might design it to run, say, 25 instances of the game at the same time. If, for some reason, it needs to run 100 or 500 instances (if there are 100 or 500 games going on concurrently) then all sorts of bad things will happen. Most likely everyone would experience extreme lag, but it could harm my server machine by overworking it, costing me time and money, which we already established that I don't have.

    What then if I only have 10 instances running at a time? I'm only using up 10% of my capacity. Why am I maintaining a server bigger than what is needed? I'll likely downsize the server. What if more players join, though, and the game takes off? I can't know these things for sure. Big game companies can afford to take a chance with that, but a single-man indie developer team without any sources of revenue? nope, too risky.

    That's the real glory of the system I'm using. There can be a million games in a million continents and there would be absolutely no need for a server. None at all. If the game is a total flop, fine. I didn't lose any time with a server. If the game takes off, fine. There will be no central server to lag. Players won't experience extreme lag issues. No hardware of mine will be damaged. There can be any number of players, any number at all, and there is literally no need to have any sort of dedicated server.

    Just one final thing: If somehow this game does take off and it does prove to be commercially viable, I will invest in a server with the sole purpose of matching up clients and game hosts. It would be exactly like Unity's Master Server, except i wouldn't feel bad for taking up their bandwidth. As previously stated, though, that's a big if.
     
  19. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    I can do better than that. Please, direct your favorite web browser to http://jotunstudios.com/Download.php and download the most recent version of Warfare 2525, the RTS I am making. The current version only has single-player capacity, but the functionality available there is the same as the functionality that will be available in multiplayer.
     
  20. fholm

    fholm

    Joined:
    Aug 20, 2011
    Posts:
    2,027
    I'm not inclined to argue with you, but let's just put it like this: Every RTS game in existence today, uses a Lock-Step model because all other techniques end up with either 1) Lag or 2) Desync. If you, a kid in highschool, think you have magically found a model that works that no one else of all these professional has not thought of during all these years, then well... I'm not going to waste any more time here.

    Edit: One last thing, the reason you think your model work is because you have not implemented it fully yet, that's the rub with networking, you thinking "this should obviously work", but it never does. Stay to the tried and true techniques.
     
  21. Apex5

    Apex5

    Joined:
    Mar 23, 2012
    Posts:
    19
    I agree with fholm on this; if you don't have lockstep the clients will all start what is essentially the same command at different times because of the delay between the communication (RPC calls in this case) reaching each computer and so battles could end up entirely different on different machines due to a accumulation of minor delays.
    From what I've seen playing with my friends in the local area it doesn't effect the game that much; but if I were to play with international players or someone gets a lag spike it would screw everything up. Also it's very easily hackable.
    Don't mind us experimenting, it's how we learn as humans: even if we do silly things along the way, thanks for the info though it'll definitely help when I get to making a proper networked rts ^^
     
  22. DethRaid

    DethRaid

    Joined:
    Aug 29, 2010
    Posts:
    209
    Hmm... you're right about that. I had forgotten to account for network delay. Re-reading that 1500 Archers article, I've re-designed my system.

    There will be a coroutine running on the GameManager (script that keeps track of which players exist, have buildings of units, etc) which will accept messages and send out a message that it has received a message. Each message received will have a number which represents the message executing turn it should be executed on (as explained in http://www.gamasutra.com/view/feature/3094/1500_archers_on_a_288_network_.php). The message turn length will by (ideally) 100 ms, I think, given the network capabilities available today compared to back in 2001 and the expectations I think people have. I can probably use Network.time to keep things in sync. A question about that, though: Is Network.time the time since the server was started, since the game was created, since a given player joined, or what? The Unity docs are, as always, less than helpful here.
     
  23. UnlimitedEdition

    UnlimitedEdition

    Joined:
    Feb 27, 2012
    Posts:
    287
    You need to make a multiplayer RTS for school. Dang, that sounds like a lot to ask of one student.
     
  24. CrzypNda

    CrzypNda

    Joined:
    May 21, 2013
    Posts:
    3
    Starcraft 2 and the original Starcraft use peer to peer UDP. There is no authoritative server keeping things in sync.

    Sames goes for Command and Conquer games. All peer-to-peer.

    Realistically if you want a playable, responsive RTS, you have to use Peer to peer. Authoritative client-server is too slow for any form of competitive play (less than 100ms response time).

    http://www.teamliquid.net/forum/viewmessage.php?topic_id=117158