Lots of little files

Posted By: Dooley

Lots of little files - 01/08/14 13:15

I am creating a sort of in-game database, which will store information collected by the player. The player will also be able to write notes and change information during game-play.

In order to accomplish this I'm using a sequence of file_str_read and file_str_write commands. For a lot of the information, I can just write it in one big txt file, and access it when I need to . However, for any strings that the player gets to change, or rewrite, I create a separate file, with a unique filename, for that one piece of data.

I am concerned about creating so many different, individual files, but should I be? Is there anything wrong with making lots of files, possibly hundreds or thousands of small text files?

If so, my question is this: Is there a way to rewrite data in a file without messing up the other information stored in the same file?
Posted By: TehV

Re: Lots of little files - 01/08/14 13:37

Ideally you should try storing all your data in one large file rather than many small ones. A lot of small files take up more disk space than a single large one with the same content, and a lot of small files look messy.
You could read all of the file into a string, read the value you want to change, do a search&replace of that value and replace it with the new value and write the modified string back into the file. To help you not get the correct value you should create a specific format for storing your data (try writing an XML file, for example).

Is it not possible for you to use the game_save() functions that are already included in Gamestudio?
Posted By: Quad

Re: Lots of little files - 01/08/14 15:24

Use something like sqlite.
Posted By: Uhrwerk

Re: Lots of little files - 01/08/14 17:21

Originally Posted By: Dooley
I am concerned about creating so many different, individual files, but should I be? Is there anything wrong with making lots of files, possibly hundreds or thousands of small text files?
No. If it works for you do it this way.
Quote:
If so, my question is this: Is there a way to rewrite data in a file without messing up the other information stored in the same file?
Only as long as the length of the data sets to be written is known and does not change. If you have changing data you need an extra layer of complexity, like using JSON, XML or something like that. As long as your first idea works out I wouldn't do that.
Posted By: Dooley

Re: Lots of little files - 01/09/14 00:14

Thanks for the advice! I think I will keep it the way it is for now. If it becomes a problem, I will revisit again later.
Posted By: Dooley

Re: Lots of little files - 01/09/14 00:17

I am using game_save for saving levels. however this is a sort of database of info that the player collects during the game. The information needs to be accessible outside of an individual level, and this seemed the best way to accomplish that.
Posted By: fogman

Re: Lots of little files - 01/09/14 09:12

http://www.opserver.de/ubb7/ubbthreads.php?ubb=showflat&Number=347690

Maybe this will inspire you.
Posted By: TehV

Re: Lots of little files - 01/09/14 14:22

If you use info variables (variables that have their name end with _i) then you can easily use game_save to store your database. All you need to do is give SV_INFO as the mode:
Code:
game_save("database",0,SV_INFO);



To save your level, you give SV_ALL-SV_INFO as the mode:
Code:
game_save("mylevel",0,SV_ALL-SV_INFO);



You then load your info save file back right when you start the game. Because game_load only changes things that were actually saved in the file you're loading, you can easily save and load your info (database) file at any time.
I assume your database has an undefined size, so you could define an array that has a length of a few thousand values like so:

Code:
var mydatabase_i[65535];



If you happen to use up all 65'535 values in that array, then you can always increase the amount of items in it.


Why spend so much time writing into custom file formats when you can just use what lite-C already gives you?
Posted By: Superku

Re: Lots of little files - 01/09/14 14:31

Because game_save/load does have a lot of limitations to the point that it simply gets unusable once you create anything (non-entity related) dynamically (using it only for info variables may work though). If you write your own stuff you have full control over it.

Although I'm a fan of global variables and arrays you shouldn't create and write such huge arrays in your script, always try to keep them low and reasonable.
Posted By: EpsiloN

Re: Lots of little files - 01/09/14 16:32

There's no bad way to do this, because the number of files wont harm the system, unless it goes to extreme limits laugh I've had 100's of 1000's of files at some points.

What I did in my latest project is to use a database for information, storing 100 000's entries and communicating through a PHP file (for security) that connects the database and to the application. This is in case you'r going Multiplayer.
Otherwise, dont worry, small text files are OK.

And, it should be possible to change only parts of those files... I didnt really understand what stops you?
Posted By: Dooley

Re: Lots of little files - 01/16/14 10:17

Hey, thanks for all the help. As I mentioned, I'm going ahead with lots of small files. However, I will be revisiting this issue again.

I think I don't fully understand how to find and replace data in a file, so that's why I'm doing it with individual files at this point.

Also, as Superku suggested, I'm creating lots of things dynamically, and this seemed to be the easiest way to create data that is available to the player regardless of what particular game level he is currently on.
© 2024 lite-C Forums