Friday, June 15, 2012

Starting the Server: smart fox lobby example

I'm developing a game that will be played online. It uses Unity as the game engine, and the networking portion is done with smart fox server 2x. Be kind on the parts where I am vague. I am trying to not let on as to what I'm actually building, but I want to create a little tutorial to coincide with the smart fox documentation as well as monitor my progress in this blog. This is my first post, and I've already done a good portion of the programming. As the need becomes apparent, I will explain things I have done already.

So the first thing you'll need to do is download and install the latest version of Unity. Then go to www.smartfoxserver.com to download and install the free smart fox server 2x community edition. We have chosen to develop our Unity scripts in C#. Some of these scripts also act as client code which connects to the smart fox server. Now as it stands, we have started to use a free Amazon server, but it's not fully functional yet. I've read that we should have Right Scale to do it, but we would rather install and manage the server ourselves if possible.

When reading the smart fox documentation, you really have to know exactly what you are looking for. I would suggest reading most of the basics first (you will be rereading most of the documentation multiple times). Then, download the Unity examples and attempt to run them... you'll probably not understand at all what to do on your first go: I didn't. But after a little experimentation, I started to understand what is happening. Most of the examples have a login scene with a connector script. I'd suggest starting with the Lobby example. Now in the Unity inspector you can select which server ip address you want to connect to. You actually have to manage this ip address in the inspector and NOT in the script itself. The serverName is declared as a public variable in the script. This means that when it's value is set in the inspector, it overrides the value that is present in the script. The default is 127.0.0.1 which known to most network programmers as "local host." If you and a friend want to connect to each other, then one computer will be the server, and one computer will be the client. The server computer will connect to the local host. If you're using windows then go to the start menu and type cmd. When it opens a dos window, type ipconfig. Scroll and look for your ip v4 address. Otherwise check out www.whatismyip.com This is the address that your friend will type into serverName in their login scene in Unity. Make sure the host computer has started the smart fox service. You can check by going to your web browser and entering the url localhost:8080 and opening the smart fox admin tool. The default user name and password is sfsadmin.

Now, there is a good chance that your program will throw an error message and not connect to the server. This is an error anytime you have to load a config.xml file. It won't do this in the Unity Editor or a web browser. Essentially, the way it works in the browser it won't fetch the file. Check here for more info http://www.smartfoxserver.com/forums/viewtopic.php?t=13126 Go to your build settings and instead of running the project in the Unity Editor, set it to build for the pc/mac. Build and run it, and you should see that it's connected to the server. In a later post I will figure out a workaround so that you can hard code these settings into the project and thus enable it to run in both the editor and browser. So from here, you should both be connected to the same server and be able to chat with one another. Great!

Please comment with any suggestions, thoughts, or insights. Have a more in depth question? Use the forums at http://www.consortya.com/forums/ and check out my personal website at www.GuitarRpg.com

No comments:

Post a Comment