SPLAT DEATH SALAD blog post, SPECIAL MAKING-OF EDITION!

— GAME: —

so I made a a game for 7DFPS called ‘SPLAT DEATH SALAD’, it’s a multiplayer unity game, and if you’d like to give it a go, head here and tell your friends to join you :).

— MAKING-OF: —

So a few people have asked me to cover how I went about doing the networking in unity and I said I’d make a post explaining it. it’s one of those issues that is a little tricky to get your head around in one go, so a lot of people (including myself) have put off learning it. but I set aside a week before the 7DFPS event to do just that, and it’s really not as scary as it seems. so here goes some talking about that!

The first thing to know is that the unity documentation covers everything you actually need, that’s kind of what it’s there for.

you should probably go ahead and bookmark this page right now 🙂

also, you’re welcome to look at the latest source for the game, or even the earliest backup I have of working networking if you want less clutter.

so then, the thing that often scares people off is NAT punchthrough, partly because anyone who is familiar with network gaming knows it’s a problem, and partly because the unity docs talk about it at some length. but there really isn’t any need to be scared, since unity takes care of it. here’s the code I use to initialise a server:

bool useNat = !Network.HavePublicAddress();
Network.InitializeServer(32, 25000, useNat);

not exactly a nightmare is it? and a little after that OnServerInitialized() will fire (assuming no errors). it’s at this point we can register our server with unity’s master server so other clients can find us (fyi; the unity master server is free to use for any and all unity projects).

void OnServerInitialized() {
   MasterServer.RegisterHost("name clients search for", "server's game name", "comment");
}

there, that’s a client set up! that’s one half of getting players connected down, the other half is getting a client to connect to that server. this is pretty much just as easy, first we ask the master server for a list of available servers to connect to:

HostData[] hostData = MasterServer.PollHostList();

we display this info to the user and let them pick a host from the list, then connect like so;

Network.Connect(hostData[selectedHostIndex], "password string");

and after that, OnConnectedToServer() will fire (again, we’re assuming no errors). at this point, we have 2 (or more) players connected across the internet. yay! as you can see, it really isn’t anything to be afraid of. I had laid out a week to explore this stuff, but I had the basics covered in about an hour without much difficulty. I just needed to get stuck in and stop dilly-dallying (like most all game-dev problems really!)

before I go onto how to get these players talking now that they are connected, I just want to point out it’s important to make sure you disconnect when players quit the game, something like;

void OnApplicationQuit(){
   if (connected){
      if (Network.isServer) MasterServer.UnregisterHost();
      Network.Disconnect();
   }
}

Ok, for sending information between players, I use RPCs (Remote Procedure Calls), this is one of 2 ways of sharing information between players, the other is State Synchronisation. but I’ve not tried that, RPCs were enough for me 🙂

RPCs basically work like function calls, you say “Run this function”, pass it some parameters, along with just who should run the function, and then the function is run for everyone who gets it. here’s an example:

networkView.RPC("LogMessage", RPCMode.All, "Hello World!");

what this does is tells everyone connected (because the RPCMode is ‘All’) to run the LogMessage() function, and pass it the string “Hello World!” as a parameter. so if everyone has a function set up that looks like;

[RPC]
void LogMessage(string msg){
   Debug.Log( msg );
}

then everyone connected will get “Hello World!” printed to their output log. oh, and the [RPC] is specifically a C# thing to make it so the function can be fired by RPC calls, for javascript I think it’s @RPC instead.

you can send a bunch of different things in RPCs; strings, floats, ints, Vector3s (maybe more types, I forget!). and AFAIK you can send as many parameters as you like too. so I use them for everything, players requesting match information from server, server providing match information, announcing that a player fired a bullet along with where, what type of weapon and what direction. I even use it for players to say where they are, where they are looking, what direction they are moving in etc.

it’s important to know that some of the RPCModes are buffered, that means if you send a buffered RPC call, it will be called for everyone who connects, even if they connect *after* the player who sent the call has disconnected. this can be pretty useful in some cases, I use it so players joining and leaving announce their information (avatar styles, player name), so whenever anyone joins they see all the players who are already connected without having to ask the server with their own RPC call.

that’s more or less it I think, obviously there’s more advanced stuff you can learn, but you can make a game with this stuff so it’s plenty, and if you want more you already bookmarked the networking part of the unity manual right? 😉


17 Comments on “SPLAT DEATH SALAD blog post, SPECIAL MAKING-OF EDITION!”

  1. PerseP says:

    Hi,
    nice game but the link to the Earliest Backup is broken

  2. Furkan says:

    spatula spatula spatula !

  3. Chris Murphy says:

    Thanks for this, the introduction to networking is really helpful. 🙂 Also, the game still kicks ass. :p

  4. shoe says:

    this game doesnt let me join ANY server

  5. josh says:

    hey sophie i played the game but i can’t go to full screen so plz help + i like your name

    • Sophie says:

      Hi Josh, there is a button to make the game go full screen in the pause/options menu (the button is on the lower left). hope that helps! 🙂

  6. josh says:

    HI SOPHIE ME AGAIN I DID THAT BUT MY SCREEN WENT BLACK BUT THEN IT GOES BACK TO NORMAL SCREEN THEN THE GAME TURNS WHITE THEN IT GO BACK TO THE GAME I TRYED RESTARTING IT AND TURNING OFF MY COMPUTER AND BACK ON AGAIN BUT IT SILL WON’T WORK PLZ HELP SOPHIE IT WORKED BEFORE BUT NOW IT WON’T PLZ HELP SOPHIE I LIKE YOUR GAME I GAVE IT 5 STARS I HOPE YOUR HAPPY

  7. Salad Fingers says:

    I totally approve this spatula. This rusty spatula.

  8. Geckoo says:

    Hi Sophie. I have a little problem with the animations. When i compile your source, i see many problem with the movements and this information “the animation state (idle/run and others) could not be played because it could’nt be found”. I think animator is not exported in source. How i can fix it? Thx for sharing ++

    • Sophie says:

      yeah, unity 4.x freaks out when re-importing animations from 3.x projects. either it disregards the animation information entirely, or doesn’t make a ‘take’ for it to sample the animations from. you’ll need to set them up yourself I’m afraid, and I can’t remember quite which frames are which… your best bet is probably to import a default take, and pick where it looks like the animation clips should be ^__^;

  9. Geckoo says:

    Ok I understand. Probably, I could find a previous Unity version to (re)compile. Otherwise I am going to try to understand manipulation with U4. Thanks a lot for advice. I wish you a good continuation ++

  10. MonsCamus says:

    That’s really useful, thanks Sofie, I’m going to download the Visual Studio sample for making your own master server and see how it goes.

    I tell you this is a lot easier than the old Sockets rubbish I had to do before…

  11. Arnold T. says:

    Hey Sophie,

    I opened the game in Unity 4.2.1 and I can’t join any public game servers. Why is this? How could I fix it so I could join public servers?

    Thanks
    Arnold

    • Sophie says:

      Hey Arnold,

      I’m keeping the name the game identifies as in the official downloads and on kongregate secret, the source files have a different name that you can change to whatever you please, and you will be able to play with others using your build of the game, but never with people playing on my builds.

      This is largely in order to prevent people downloading the source, changing it so that they are super-powered and then joining “official” (if that word even applies :P) games.

      The source is there to learn from, or so you can make your own *separate* version of the game, I just can’t allow the possibility of letting people use it to exploit the versions of the game I release myself ^__^;

      I hope that explains why things are how they are!
      – Sophie

      • Arnold T. says:

        Hi Sophie,

        Thanks for explaing, I can see why you would do so.
        How do I change this identifier in my own game so I can export a copy of the game which other people with your source wouldn’t be able to join?

        Cheers
        Arnold

        • Arnold T. says:

          Edit: Is the game identifier in _SophieNet? Is it on the SophieNetworkScript under UniqueGame Name? On top of that, could explain the variable Game Name to me?

          Cheers!
          Arnold


Leave a Reply

Your email address will not be published. Required fields are marked *