- Home /
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".
Answer by duck · Apr 21, 2010 at 07:14 PM
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.
So, from my understanding you are telling me that I would need to direct the client to a configuration file (PHP / ASP) that in turn would allow the client to connect and store data via a database?
No, your client would interact with a multiuser server application (like Smartfox Server). That server-side application would then deal with the database. It would act as a 'middle man' between your client game instances, and the database itself. The client would never connect to the DB, nor supply SQL commands.
@Travis - This is also called n-tier or 3-tier architecture when developing applications such as webapplications. Client -> Server -> Database (3 tiers)
Would this structure still be recommended for offline games? I'm not doing an $$anonymous$$$$anonymous$$O, but rather a tile-based turn-based strategy game.
@mrliioadin no. if you're offline, you'd have no way to connect to the server which interacts with the database (unless you're planning hosting the database locally as well?). I'd look into X$$anonymous$$L for storing local data, though you could also use sqllite.
Answer by Island Bill · May 18, 2015 at 03:21 PM
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 ;) ).
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.
Answer by Daniel 15 · Feb 10, 2011 at 11:29 PM
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
I have came across your tutorial before. As far as i am concerned, it is a nice tutorial BUT it does not deal with the middle man. All that is happening on your script is UNITY is trusted with information that the user can then get.
I think and its a guess the user needs to do an include dbconnection.php
or something
@followmyfaith666 it's still the correct answer, it answers the question. The question was not "can I have general information on how server authority works". I appreciate people addressing that issue for the OP, but as a person googling, I would appreciate the question ASKED to be answered first, and that then they address what you think would be a better route for the person coding.
Your answer
Follow this Question
Related Questions
Host unity game with photon 0 Answers
if(SQL SELECT) { do something; } ???? 2 Answers
simple online storage for game? Any other way? 1 Answer
Best practice for a turn-based game 1 Answer
Database connection FPS game? 0 Answers