Game Development Community

SQLite Help

by none · in Torque 3D Professional · 06/26/2012 (11:05 pm) · 15 replies

Hi! I just am getting started with T3D, and I've had TGE before and used it for years so I get the general gist of how to compile ect.

I implemented this resource:
http://www.garagegames.com/community/resources/view/18425
because I want to have an SQLite database for my game. The resource fit in perfectly and I have the build complete without an issue.

Now my question is, I want to create a feature where upon start-up, the user is prompted to either create an account or login. I can create the nonfuctional part of the GUI, but what I need help with is how to store the created username/password into the SQLite database, and then allow someone to login to their account also using the database. Eventually I will store the player's kills, deaths, equipment ect, but I'd really like to try to implement this to get a solid starting block, and I'm sure that others would be interested in this as well.

Any help is really appreciated, I have no idea where to start with SQLite but I would like to learn. Thank you for any help!

#1
06/27/2012 (1:34 am)
torque is not directly supported databases, written at times codes who works well on single player, but they had serious problems with multiplayers.I not found a credible renewal for T3D.
Better developed your game recording the data in files and later do the database connection.
I load the file data to memory (for 1200+ player accounts and player data) in the MMO that I write and has no problem.
Start with text files and step by step go to binary files.

we see in the future :)
#2
06/27/2012 (10:08 am)
Thanks Dimitris! Could you point me in the write direction for writing files through script. For example, I want when the person types in their Username and Password, I want it to then write a file with the user's username and password, and then further down the line store other info such as inventory ect. I'm new to scripting so any help is appreciated.
#3
06/27/2012 (10:28 am)
Simple example:

function writeDataToFile(%info1, %info2) {
   if(isFile("test.txt")) {
      echo("File test.txt exists... appending data");
   }
   %fileObject = new FileObject();
   %fileObject.openForAppend("test.txt");
   %fileObject.writeLine(%info1 @ " - " @ %info2);
   %fileObject.close();
   %fileObject.delete();
   echo("Done...");
}

Calling writeDataToFile("Name", "Player1"); gives you a new .txt file in your gam'es main directory named test.txt with the contents:

Name - Player1

As for the databases, I'd recommend doing all of the file writing on the game side, and then shipping it up to a web server using the TCPObject Torque has. You'll more than likely want either an SSL layer or some form or encryption to prevent evil people from messing with the data, but that's for more advanced scripting measures.

Good luck! and feel free to post any questions..
#4
06/27/2012 (11:31 am)
If you have a database available why write to a file?

It sounds like you just need to learn how to use the database:
www.sqlite.org/docs.html

When in doubt use Google.

Using a file will be very painful down the road. That is why databases were developed. To help alleviate that pain and better structure the data. I recommend you also study how databases work, how data is structured, and how tables interact.
#5
06/27/2012 (1:59 pm)
Alright thank both of you guys. I'm going to play with both of these! I suppose for the mean time i can use the file writing for the single player portion of my game, and experiment with the database for the online half. I'm gonna take a look at these and I'll post if I have any questions/issues. Thanks!
#6
06/27/2012 (4:30 pm)
Well if you use a database for the remote part you do not want to use Sqlite. You will want to do what Robert said to do and access the database through network connection.

You will still run into issues not using a database locally unless your game is really simple. Databases really can help you separate your data from implementation as well. This can help if you need to use a different technology besides Torque.
#7
06/27/2012 (10:30 pm)
Thanks Frank, I'm assuming a MySQL server with php would be the way to go with remote connection? Ok so I got something pretty simple working right now and I'll climb my way up but for now I have it created so that the player is prompted to create a new character and then it changes this to the new player and saves the name ect. How would I make it so that the create character prompt only comes up until the first character has been created?
#8
06/28/2012 (9:04 am)
That can be saved to a $Prefs:: variable, for instance:

// saved my first character, so note it in my preferences
$Prefs::mustMakeCharacter = "0";

Now, before you ship that variable must be set to 1, but basically anything that you store in a variable with that prefix is automatically stored in your prefs.cs file and will be available.

So when your customer first starts the game $Prefs::mustMakeCharacter should be "1". You test for this condition, pushing the NewCharacterGui (or whatever) if it is true. After creating the character you set it to "0" and it gets saved automatically. The next time you start, the value should remain "0" and your logic should bypass the NewCharacterGui section and go on to CharacterSelectGui (or whatever).
#9
06/28/2012 (9:09 am)
Awesome! Thanks Richard, I got it working perfectly!
#10
06/28/2012 (1:16 pm)
Ok one more question! For right now, because I'm just learning and with my capabilities anything I make will be simple, I'm handling my saving through the prefs.cs. Now my question is, when I have this game all ready for publishing, is there anyway to have this encrypted like in the old TGE. Secondly, if it were encrypted, is it possible to still update the variables within it through events within the game? Thanks!
#11
06/28/2012 (4:22 pm)
Well, you might be able to use the zip library to save this out and read it back as an encrypted zip file. However, if you don't store any actual game data in the prefs file you shouldn't really need to encrypt it. If your game data is all stored in the database it should handle all of that for you anyway.

Additionally, you should be able to use encrypted zip files for any local storage that you might need. I haven't really played with it, but the system is supposed to allow use of zip files as a sort of virtual file system - you could treat a zip file as if it were a folder and just write files to it.
#12
06/28/2012 (5:00 pm)
Ok that makes sense! I'm going to read this up and see if I can't get it working! I'll post a picture of what I've been working on soon! Just getting a few kinks out!
#13
06/28/2012 (5:41 pm)
@Krystian,
If you use a database you can do a query to see if any characters exist. If the query turns up 0 (zero) rows then you don't have any characters or savegames.

A while back I was experimenting with the Sqlite resource with TGE. I was able to save the level on exit (of course this should be a menu option of some sort) and I would store the position of some objects. When I reloaded I even would set the angle of view of the objects (player objects) so that it was as if I had never left. It worked really well. It reduced my object loading for certain objects to just a database query and recreated them when the level loaded.

Edit:
At some point I will do complete levels as well all database driven. One of the reasons for this is you could literally download a level and it would just be a database file.
#14
06/28/2012 (6:26 pm)
That's really cool Frank! I'm reading the SQLite tutorial and I'm still trying to get the ropes of it.
#15
06/29/2012 (12:55 pm)
If you get stuck just post your query here and we can help you sort them out. They are not really that hard, it just is not the same as writing script or C++.

Just keep your tables simple and use unique integer keys to identify objects. That way people can change their username, but the rest of the data stays the same as it relies on the key and not the username.

For instance I would create a user table like this:
[unique_key],[user_name],[first_name],[last_name],[email]

From then on any table with data pertaining to this user would use the unique key to identify that information. So any of the rest of the user data can change. So you could have a save game table:
[unique_key],[user_unique_key],[savegame_name],[save_filename],[snap_shot_filename]
The user unique key would actually be the unique key from the user table. So you can just query on the user unique key to see all the save games for that user.

Notice how each table is simple and stores specific data pertaining to the user. Also notice that I am not trying to make one big table for everything. I am making specific tables for specific purposes.