External Database?

Started by LordHart, Tue 16/03/2004 04:07:50

Previous topic - Next topic

LordHart

I was wondering if there is a way to have a database with some information in a .dat file or such. Which could have a few different attributes seperated by commas and each entry into it seperated by a ; like this...

NAME, HEIGHT, WEIGHT, AGE, HOMETOWN;
And each one of these lines within the .dat would be assigned a number, so the first would be 0, the second 1, and so on...

So that is in the .dat file and is the first entry of the database. Where you could then assign something like GetData(filename.dat, 1, 3); which would read the filename.dat and then take the WEIGHT of the second line in the file... and show the value of that attribute...

Or you could SetDatabase(filename.dat), and then use GetData(1, 3); which would do the same thing...

Does anyone get what I'm saying?

Is there anyway to do this, or am I just jerking it?

Scummbuddy

have you looked at 'File I/O functions' in the manual? is that what you are asking about?
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

LordHart

It sounds similar, but by reading the stuff in the help file, it sounds like you would only be able to open an entire file for reading or writing, and not taking a single stat from it.

Scummbuddy

how about an internal structure? its not really supported, but i do believe it works within ags.
- Oh great, I'm stuck in colonial times, tentacles are taking over the world, and now the toilets backing up.
- No, I mean it's really STUCK. Like adventure-game stuck.
-Hoagie from DOTT

LordHart

How would that work? Would it be similar to how I did that crappy example thingy?

Migs

#5
If you're creating a database, use a primary key, like a unique integer (recommended), or if all your names are going to be different, you can use that as your primary key (not recommended).  Ideally, you would be able to use linked lists and so on to create your database, but AGS [currently] doesn't support features like this.

First, you could have AGS create the data before the game starts up, or create it yourself in another program.  Don't make it a delimited text file, since that's not secure at all (unless you don't care if players can edit the file), but use raw integers and strings.  You could implement this several ways, but the algorithm would look like this:

1. Open file.
2. Write integer for unique ID.
3. Write string for name.
4. Write string for height.
5. Write string for weight.
6. Write integer for age.
7. Write string for hometown.
8. Repeat steps 2-7 for all entries.
9. Close file.

To find an entry, you're not going to get the functionality of SQL in AGS (that would be a great plug-in, though), but you can use a simple search algorithm to search through the whole database one by one:

1. Open file.
2. while (unique_ID != desired_ID) do steps 2a-2f
2a. Read unique ID.
2b. Read string for name.
2c. Read string for height.
2d. Read string for weight.
2e. Read integer for age.
2f. Read string for hometown.
3. Close file.

I've tested this method out in my own external database constructed this way, with over 200 entries and about 40 integers/strings per entry, and it runs fast enough.  You might have some speed problems if you have to search through around 10,000 entries.  I'm not quite sure if you could implement another search algorithm, like a binary search, which would be considerably faster.  Anyone else have any ideas?

EDIT: If the database is quite large, you might want to consider splitting it up into different files, then having the system access the correct file, thus reducing search time.
This signature intentionally left blank.

SMF spam blocked by CleanTalk