Database Driven Games
by Matt Fairfax · in Technical Issues · 02/26/2001 (8:54 am) · 9 replies
I was implementing a simple text parser so I could read initial values for my game from a text file the other day. I was pondering on how to make the process more efficient and came up with an interesting idea. What if you were to store all of these values in a database and then at load time you would run sql statements against the database and get back an easily parsed string with the values. For example, my current game project has many different weapons and I store information about them like model_file, model_scale, expolision_type, damage, etc. If I were to store all of these values in a database then at load time I could simply run a statement like "select (*) from weapons_table" and it would hand back a string of characters containing all of these values for all of the weapons. I could then initialize the internal variables with these values. I then began to think about how I would go about creating and adding on to this database. Since I am pretty handy with Access my thoughts naturally turn there first but I wouldn't want to force a developer to use a piece of software so blatantly not portable to other platforms. I also thought that it would be cool for a game designer to be able to add weapons to the game w/o any help from the programmers. For some of the values he could pick from a list box of all the available values. For example, say he were creating a new weapon and he went to enter the expolsion_type. He could click on a drop down list containing "FIRE_BALL, ICE_STORM, LIGHTNING_BALL". The values in here would be queried from somewhere else in the database where they had been entered by the programmer once he had added these special effects to the game. But if you were to do this then you would want some sort of security in the database. After all you wouldn't want the designer just adding METEOR_STORM to the list when that effect doesn't exist. Access only allows you to do security on the database as a whole not in indiviual tables. You could get into linking databases but this would quickly get messy. Thinking about the security reminded me of the security stuff I was recently learning with ASP. It also reminded me of the security you could implement using PHP (much like this site). Both ASP and PHP have excellent databasing capabilities. Another benefit of using them would be that all the designer would need is a web browser to do stuff in the database. He could then use any OS he likes. There are a lot of things that you could expand this idea into. You could allow a designer to script more advanced effects from a list of simpler effects built into the engine. You could script basic behaviour of some AI entities. You coul have artists uploading files much like on this site and having the database organize them. There are a lot of things you could do with this. It would also greatly help to organize projects with enourmous amounts of data. There are several good ways for a programmer to query data in and out of the database. There are libraries that'll handle it on tons of platforms. If you are on Windows adding database support is trivial with some MFC calls. You can also use the source from PHP to build a library of your own or you can link Python directly into your app and use its database functionality.
I am planning on trying something out like this in my current project so I will let you know how it goes. I'm sure someone else has already thought of using this method but it was new to me and I wanted to share it.
I am planning on trying something out like this in my current project so I will let you know how it goes. I'm sure someone else has already thought of using this method but it was new to me and I wanted to share it.
About the author
I am a Game Designer at PopCap who has worked on PvZ Adventures, PvZ2, Peggle Blast, and Bejeweled Skies. I am an ex-GarageGames employee who helped ship TGE, TGEA, Torque 3D, and Constructor.
#2
Jeff Tunnell GG
02/26/2001 (9:32 am)
This is right on. As Tim and Rick were implementing the GarageGames site in PHP and SQL, we got off on a tengent of doing a huge MASSIVELY multi-player game based on this system. If we ever get the time, we may still do it.Jeff Tunnell GG
#3
Anyways, back to the topic:
I hadn't thought of the dbase idea up till now, I've been using a linked list and just searching through that. I'm guessing my way is probably slower, anyone know for sure?
Andrew
02/26/2001 (11:48 am)
My first post, and wondering if there used to be more fields, or if there are going to be more...Anyways, back to the topic:
I hadn't thought of the dbase idea up till now, I've been using a linked list and just searching through that. I'm guessing my way is probably slower, anyone know for sure?
Andrew
#4
If you don't get the O(n), it's basically a time function. O(1) is the fastest, and is stuff like hashtables and arrays. Trees are like O(ln(x)) or something...I forget.
02/26/2001 (1:20 pm)
Linked list is as slow as it gets, that's O(n) search time, IE linear. So just about anything is faster.If you don't get the O(n), it's basically a time function. O(1) is the fastest, and is stuff like hashtables and arrays. Trees are like O(ln(x)) or something...I forget.
#5
The method I use now to parse a text file and get a value is to call a GetLine() function that returns the characters of a given line. The I use sscanf() to parse the characters into variables names and values. Something like:
unsigned char *string;
int width;
string = GetLine();
sscanf(string, "Width = %d", &width);
I haven't gotten around to it but it would be trivial to create a couple of functions like GetInt(), GetFloat(), and GetString() where you would hand them the string of the value you want. They would then search the file for the string and get the line it is on, then parse for an int, float, string, etc. and return them.
What are you doing with linked lists?
02/26/2001 (1:21 pm)
*shrug* As far as I know, no one has built a game that used my database idea. I know that Bioware uses a database to organize their content (have to when you start exceeding 40 Gb of artwork =) but I'm pretty sure that they don't actually use it in the game. I know that a lot of database libraries and apps are designed to get a lot of records very fast. You have to when you might get 10,000 records back at any given time. Most games will request more than a couple hundred records so it should be pretty fast.The method I use now to parse a text file and get a value is to call a GetLine() function that returns the characters of a given line. The I use sscanf() to parse the characters into variables names and values. Something like:
unsigned char *string;
int width;
string = GetLine();
sscanf(string, "Width = %d", &width);
I haven't gotten around to it but it would be trivial to create a couple of functions like GetInt(), GetFloat(), and GetString() where you would hand them the string of the value you want. They would then search the file for the string and get the line it is on, then parse for an int, float, string, etc. and return them.
What are you doing with linked lists?
#6
02/27/2001 (7:38 pm)
Hehehe, figures I've only really gotten an understanding of the slowest way ;) Can anyone point me to a good tutorial that shows how something faster works?
#7
To be quite truthful, this game could be a great learning experience for you...however there are things that will/could happen that will totally throw you for a loop, and I think that it would be best, in this situation, to use work that is already done. Try making the game in PHP and use a SQL server to do your database stuff for you. SQL already has synchronization, data structures, queries and all that good stuff (plus more) taken care of for you. If you want security, relyability and free...use Linux, PHP and MySQL to do it. If you have a Windows server, you can run MySQL, Apache and PHP on that as well, and it will be the same thing, it's just that Windows is inherintly unstable.
If you're serious about coding this yourself, there is a lot...wicked lot of stuff you have to condsider, and I'll write that up in another post if you'd like.
03/02/2001 (8:08 am)
It depends on how complicated you want to be here. I would suggest that before you embark on this, read a good book on Data Structures. If you can get one that uses C, that's good, because it will explain everything and how to make that datastructure because unlike Java and C++, C doesn't have any built in classes to do work for you, so the book should show you how each data structure works. To be quite truthful, this game could be a great learning experience for you...however there are things that will/could happen that will totally throw you for a loop, and I think that it would be best, in this situation, to use work that is already done. Try making the game in PHP and use a SQL server to do your database stuff for you. SQL already has synchronization, data structures, queries and all that good stuff (plus more) taken care of for you. If you want security, relyability and free...use Linux, PHP and MySQL to do it. If you have a Windows server, you can run MySQL, Apache and PHP on that as well, and it will be the same thing, it's just that Windows is inherintly unstable.
If you're serious about coding this yourself, there is a lot...wicked lot of stuff you have to condsider, and I'll write that up in another post if you'd like.
#8
03/16/2001 (11:58 pm)
My mud uses a PostgreSQL backbone. Because of this we can use hash search functions which are super quick. By doing this instead of loading stuff from files and into linked lists our ram overhead is a lot lower, and load times are just as quick if not quicker. Not only that but PHP and Perl scripts can interface with the database, as well as a slew of other apps, like mod_auth_pgsql for apache. We can also move our DBase to a new location should the need arise, and leave the server in the same place, referencing the DBase as necissary. Now whenever i look at a game im gonna do, I think, how am I going to put a DBase into it? You should know that running some databases, like PostgreSQL requires a lot of time to learn, as well as administer. But the payoff is worth it.
#9
Having said that, you could always prototype your game using MySQL.
One thing that scares me is that SQL can do a LOT of stuff that you havent got a lot of control over. As far as I can tell, the "big players" in MMORPG's all use Oracle for its distributed transactions, typically they'd use stored procs to speed things up (oh yeah, you MUST use stored procs if youre going to get any performance). This is such a specialised job, that youre going to need a LOT of work to get anything approaching MMORPG's working.
My current feeling is that I'll play with a totally distributed solution when I get a chance (distributed in a pretty similar fashion to how the internet itself works). We were working with a pretty nice distributed server system for my engine (codename freedom, and pretty much like the V12) before the V12 announcement, so its something I might put forward for inclusion in V12 if we decide to keep the original design we were working on.
In the end, I'm more interested in writing a game and making a living from it than writing a library and having someone go "wheee its great" :))
Phil.
03/21/2001 (11:58 am)
Well, Ive toyed with the idea of doing a typical RPG setup using MySQL at one point, but MySQL isnt really scalable enough, basically if you want to have a lot of clients working, then you need to use a distributed database (i.e. one that supports distributed transactions). Which kind of rules MySQL out.Having said that, you could always prototype your game using MySQL.
One thing that scares me is that SQL can do a LOT of stuff that you havent got a lot of control over. As far as I can tell, the "big players" in MMORPG's all use Oracle for its distributed transactions, typically they'd use stored procs to speed things up (oh yeah, you MUST use stored procs if youre going to get any performance). This is such a specialised job, that youre going to need a LOT of work to get anything approaching MMORPG's working.
My current feeling is that I'll play with a totally distributed solution when I get a chance (distributed in a pretty similar fashion to how the internet itself works). We were working with a pretty nice distributed server system for my engine (codename freedom, and pretty much like the V12) before the V12 announcement, so its something I might put forward for inclusion in V12 if we decide to keep the original design we were working on.
In the end, I'm more interested in writing a game and making a living from it than writing a library and having someone go "wheee its great" :))
Phil.
Associate Matt Fairfax
PopCap