Sunday, June 17, 2012

Multiple Unity Scenes and Amazon EC2

Hey followers and new viewers. Today, I will review how to develop an overall structure for your Unity project that's networked with Smart Fox Server 2X. I'll also point you towards starting a server that can be accessed over the web.

I've noticed so many subtle features of Smart Fox that aren't explicitly stated in the documentation. That is why it's important for you to examine the code in the examples: found here SFS Unity Connector. When you look in each example's folder, you'll notice that it contains a deploy and source folder. The deploy folder contains the client which has been built to run in a web browser. The source folder typically contains a Unity package; the "First Person Shooter" and "Tris" example also contain a server folder which holds Java files necessary for maintaining the game on an external server. I discussed the Connector and Lobby examples in my last post, so today, I suggest that you tinker with "Object Movement" which is an example of networked game play that is Client to Client.  Create a new project in Unity and load the package by clicking Assets> Import Package> Custom Package. Once it is imported you will have access to all the scripts and assets for the project.

This example uses multiple Unity scenes which is why we're looking at it today. The architecture for this is that the first person to log in becomes the server. The data for all people who join afterwards is maintained by this first person. If your game doesn't require all that much movement, then this would be a decent architecture for you to look into further. For my purposes though, I'm showing this example so that you understand how to manage a smart fox server through multiple scenes and scripts on the client side without having to use any Java files. However, a game like the MMO you are trying to develop will benefit from having a true server / client architecture that uses Java files on an authoritative server. I'll talk about that in another post though.

The SmartFoxConnection script is crucial for maintaining a connection throughout your project. It holds a private static connection; static variables are maintained in memory until an application closes (watch out for making too many in a scene though; even when that scene is closed, the static variables will not be deleted). The first scene you'll open is Login, and you'll notice that there is a Login GUI script in the hierarchy. When it is selected, look in the inspector to see that the server Name is set to 127.0.0.1 (If you are having problems connecting refer to my last post or go ahead and build it for pc/mac which will solve the problem). When you run this program, before you even click to log in, you're client will already be connected to the server. The connection in SmartFoxConnection is then set to the connection in the Login GUI. This is exactly what you want. Now, when you log in,  you will be logged in to the room The Lobby and the scene will close. If SmartFoxConnection had not copied your connection then the new scene would not be connected. It also maintains a fantastic function on lines 34-38 that will automatically disconnect you from the server when the application closes. If it weren't for this line, Unity would crash each time you stop the application. Now, when any script containing a private SmartFox is loaded with a scene, the instance will copy the static connection that is maintained by your SmartFoxConnection script.

It's important to follow a structure like this because it streamlines the execution of your game. Once you're connected to the server and subsequently logged in to a specific room, you can close the script that has all of those event listeners for the login process. This will make it so that all you need to focus on in the game scripts are the variables and messages that need to be transferred among all the users. You will be able to send public messages to everyone else in your room. You should also automatically send a public message to users in a room once you have logged in and loaded it. This way everyone will know that you've joined. Now take some steps on your own to read all of these scripts and see how they interconnect. If you don't work hard to see how they all work together, then you won't succeed at copying the important parts to your own project and manipulating it to suit your needs.

So that about covers how to manage the connection for all of your scripts in a project. Now, I will point out some of the basics of getting the server running on Amazon. Go to Amazon web services, and you will find that they even have a free plan for 1 year. Be wary though because this plan is intended to be used mostly for testing purposes and could cost you money if you go over the limits. But basically, you need to get the server and then instantiate a Virtual Machine on it. Amazon has several Virtual Machines to choose from which enable you to remotely control a computer. We went with Windows Server 2008 RC2. Install Smart Fox on it and follow the instructions at the bottom of this page Server Installation. If you're using Amazon EC2 to host SFS2X be sure to open the TCP & UDP ports in the Security Group settings in the EC2 control panel. Also, make absolutely certain that you edit the firewall settings as suggested in the Getting Started documentation linked above or it will not let you access it from other computers. Once you start the service, you can copy the URL (excluding http://) and paste it into Server Name in your project's Unity Inspector. You can even control it using the sfs admin tool as per usual with localhost:8080 in the browser on your Virtual Machine. Great try it out with a friend with the Lobby example and you'll both be able to chat with one another.

Thank you for reading. Please comment below with any suggestions, thoughts, or insights. Follow me at https://www.facebook.com/FaytownGames  and join my circles on Google+


Have a more in depth question? Use the forums at www.consortya.com/forums/ or drop me a line at www.GuitarRpg.com

No comments:

Post a Comment