How to make an MMO using Unity3D and SmartFoxServer.
A tutorial by WarpZone. Special thanks to: ThomasLund, zeroZshadow, duke22, and everyone at #unity3D
Note: I use a mac. Mac comes with Java built-in. If you use Windows, chances are you don't have Java installed yet. You will need to download the Java Runtime Environment to run existing Java applications.
The first thing we'll do is test-run a precompiled Unity/SFS example on our system:
Installing and Testing SmartFoxServer:
- Download SmartFoxServer Pro. (NOT the Lite version! Not the Basic version! PRO!)
- Unzip it.
- Download the Multiplayer Island Demo from SmartFoxServer's API page.
- Unzip it.
- Run SmartFoxServer.app. (You should see a white Terminal window with some text. Windows users will see a black command-line window with some text.)
- Launch Unity3D
- Open login.Unity (It's in the folders from that Multiplayer Island Demo)
- Click the Play button.
If all goes well, you'll connect and be able to move around in 3D.
If all did not go well, it'll say "unable to connect" and you'll get no 3D. Make sure you are using the PRO version of SmartFoxServer, make sure it is running, and make sure the IP address you're trying to connect to is your computer's home address (127.0.0.1).
Now that you've seen what the fully-compiled Unity/SmartFox combo looks like in action, and you know how to test it from your computer, it's time to learn how to roll your own Extension for SmartFoxServer. Extensions are ESSENTIAL for turning a simple chat engine into a full-fledged MMO. Only the PRO version of SmartFoxServer supports extensions, but don't worry, you can use any version of SmartFoxServer for testing purposes. The only limit is you're capped at 20 simultaneous players. Once your MMO project starts to look like a real game, you'll be able to decide whether or not it's worth it to you to pay the SmartFox licensing fee and support massive amounts of users.
An extension is just a single .java file, which you will then compile into a .class file. Then you place this class file into the right directory under your SmartFoxServer app, and when you run SmartFox it will load the extension.
I use Eclipse to compile in this example. Eclipse is a bloated piece of crap with way too many features, but it works, and it's still maintained. While everyone in the chat channel had their own fond memories of using other, simpler Java compilers, no one could really recommend one in good conscience. So Eclipse it is.
Rolling your own version of an example Extension:
- Download and install Eclipse.
- Download the API example.
- Launch Eclipse.
- File > New etc. (See section 6.9 of the SmartFox manual)
If all goes well, you should see your own comments in the SmartFox server log, as well as the name of your extension loaded. For more examples of stuff you can do with the extension, see the SFS Extension Cookbook(link)
Now that you know how to roll your own SmartFox extension, we'll start looking at ways you can use it to turn that interactive tropical island demo from a glorified chat engine into an actual game.
Implement simple PVP combat:
- Give the players statistics: Health
- Give players the ability to attack each other.
- Server moderates combat and sends the results of attacks to the players.
- First one to lose all health is killed.
- Anyone not in combat gradually regains health over time.
This type of combat is a fundamental building block of realtime competitive games. But it lacks the hallmark of MMOs which is persistence.
Implement an experience system:
- Sign up for a web hosting account.
- Start a new database.
- When players log in for the first time, create a record for them in the database.
- Add another statistic to all players: XP.
- Winner of a PVP battle gains XP.
- When a player's XP changes, it is saved to the database.
- When a player logs in, their XP is loaded from the database.
- Ideally, the server should track these XP changes in realtime and only write to the database occasionally.
There's still a lot you could do to make the game more interesting. But at this point, you have a website, you might as well use it. Upload your Unity file and SmartFox directory to your website. Since you haven't licensed SmartFox yet, you can only have up to 20 players at a time. Password protect your game so only you and your beta testers can access it. Otherwise, as soon as some random internet surfer stumbles across the site, you'll have way more than 20 users trying to connect.
Moving your MMO from your desktop environment to the web
- Add a password to the launch of your game.
- Set up Smartfox on your server, including your custom extension.
- Upload your Unity web game to the website.
- Oh, and make sure you change the IP address from 127.0.0.1 to whatever your web server's IP address is.
Now's probably a good time to start thinking about server maintenance. If the server ever goes down, you need to be able to get it back up as quickly as possible. If someone discovers a hack, you need to be able to work around it and release a new version ASAP. You may also need moderators or game masters to police the world, ban cheaters, and help answer questions for new players. You don't need to hire people until your game goes live, but decisions you make now in the design of your MMO can have an impact on how much intervention your players will need to enjoy the game.
Okay. Now that your game is up and running, and has the rudimentary parts of any MMO-- Socialization, Gameplay, and Persistence, all you have to do is embellish on that with your own ideas, add content and more nuanced gameplay, and you'll have your own PVP MMO.
What about PVE? PVE is a bit more complicated. You need to program your extension to spawn mobs and manage their AI. You need to award XP after a mob is killed, and ideally drop gold and items, all of which are used by the players and augment their skills. You will be designing and implementing several layers of new systems, all of which combine to create PVE gameplay. These will ideally lead up to PVP for a satisfying, self-perpetuating endgame, while giving low-level players something to do and succeed at even at low levels.
Your first mob:
- Specify conditions for a mob to spawn.
- Send spawn event to clients.
- Send AI commands to clients.
- Receive player input from clients.
- Model the combat server-side.
- Display the combat client-side.
Your first loot drop:
- Spawning items is just like spawning mobs...
- ...except in the ways that the player interacts with 'em...
- Save loot to the database, just like with XP.
Further ideas for MMO development:
- Monster & loot tables on the database
- Premium accounts; premium content
- Player-entered content
- Business models
- Grinding and the endgame