How can I connect Unity to an SQL database in order to implement an MMO?

Hello, first of all I'd like to thank each and everyone who has dedicated their time into producing Unity, and tutorials, and to those who assist others with their needs.

Now then, as far as my question is concerned, what I am planning is creating a massive multi-player online (MMO), and I have a large map so far. I've read/watched most of the tutorials available, and I'm just waiting for my paycheck to invest into the Pro version of this; but I am curious as to how I'd be able to connect my client to a server, that will register play coordinates, items, stats, etc.

Any assistance on this subject would be great. Basically, I am interested mostly into connecting the client to a SQL database.

-- To make it easier for you/me to understand, I'm going to ask if you wouldn't mind explaining it, if the database was, "client".

With this kind of set-up you would generally call the the unity app the "client", because it's the part of the system which the end-user uses to view and interact with the world.

Your client would never normally have direct access to the database. This would generally represent a significant security problem. What is more typical is that clients would talk to a dedicated multiuser server application using socket connections (for example, something like SmartFox server, or often - for MMOs - a custom written server app).

It's also the case with simpler database interactions - such as a server-side scoreboard - that your client would never access the database directly, for similar security reasons. Instead, your client (the game) would talk to specially written server-side scripts (eg, in PHP or ASP), which in turn would access the database themselves.

Therefore it's only ever server-side programs that are under your complete control which have authorisation to directly access the databases, and your clients (which are less trusted, because they're in your user's hands!) are restricted to making higher level requests via an API of your own design, which is restricted to relevant commands such as "SubmitScore", and "RetrieveScoreboard" in the case of scores, or things like "MoveToLocation", "TakeItem", "Say Message", etc for a multiplayer RPG.

This multiuser server would then deal with handling interactions in your world, and it would be responsible for interacting with the database behind the scenes to create, read, update and delete information from the DB such as user details and persistent world data.

For this reason, your Unity client need never know about the existence of such things as SQL, tables, records, etc :-) These are all things which should stay on your server, either as server scripts or within your multiuser server application.

Maybe I can help clarify things a bit here for the benefit of people who stumble in here from a Google search (I know windows and Mac users sometimes have a more difficult time with separating the various parts of a network or even workstation architecture :wink: ).

It IS possible to have your game DIRECTLY connect to a database, but it is NOT a good idea. Your game script could, for instance directly connect to a mysql database and issue mysql commands and that would work, but you would be exposing your database to massive security issues.

There needs to be a layer between your gamers and the database. This layer can be written in PHP, Javascript, C, or just about any language that has the ability to open a socket connection to a running database server, and it can reside on any intermediate server you choose. It does NOT have to be Smartfox or ANY service that is currently offered (I’ve nothing against these solutions but if you can code just a little bit and follow basic security precautions, there really isn’t a need).

Example: Add a name to a database

unitygame → php script residing on an intermediate server → mysql database

Lets break that down. The Unity game script passes information to be handled, say a gamer name, to a php script. The php script knows that this information is being passed, and it says to unity “OK hang on, let me forward that request”. The php script opens a socket connection to the mysql database and says “please add this gamer’s name to the database”. Mysql then processes the request and replies with an update on that request. The php script then passes the status back to unity.

It sounds very complicated but really it isn’t. All you’re doing is putting an intermediary, sort of like and Executive Secretary, between the game client and the actual database. It’s a go-between.

I hope this makes sense.

I've made a tutorial, explaining everything, to connect and grab data of a database, chek it out! http://forum.unity3d.com/threads/77447-Starting-my-MMO-with-SQL-SERVER!-in-Javascript