Forum

> > CS2D > Scripts > File:Read/Write Funtion
Forums overviewCS2D overview Scripts overviewLog in to reply

English File:Read/Write Funtion

4 replies
To the start Previous 1 Next To the start

old File:Read/Write Funtion

KagamineLen
User Off Offline

Quote
Could someone make me a Read and Write function for a file i wanted to save my players stats to a file because shutting my server down would make my player's save back to start

old Re: File:Read/Write Funtion

J_Lucas
User Off Offline

Quote
user KagamineLen has written
Could someone make me a Read and Write function for a file i wanted to save my players stats to a file because shutting my server down would make my player's save back to start


i want learn abour it too , if any one can help me ...

old Re: File:Read/Write Funtion

KimKat
GAME BANNED Off Offline

Quote
There's already some nice file uploads that exists on this website. Try them out!

I can simply point you to them.
• file cs2d Stream functions library for Lua
• file File does not exist (12623)

Test and explore the functions that come with it, if you want further explanations on how the functions work just place that feedback here and I'll help you. I already know how some of them work.

old Re: File:Read/Write Funtion

VaiN
User Off Offline

Quote
I find that it's best to save data as Lua tables. I've seen quite a few examples of save/load functions that are using their own parsing method for loading the data. It just doesn't make sense when Lua's parser is faster than anything you can script inside of Lua.

If you search on google there are many examples of saving/serializing table data on the mailing lists and elsewhere, and why it's the best option (for saving simple data). The only exceptions are if you are working with bytes, which you really don't need in most cases, or if you need to have the data in a different format (JSON, XML, ect), which you would need a completely different solution for.

This way you only need to use loadfile() to load the data, and you already have it in a table ready to go. There are also solutions if you have multi-dimensional tables (sub-tables) as well.

If you don't want anything complex, just write the data as a string into a file. I'll give an example (no sub-table support):
Spoiler >

For a faster but more cpu/memory intesive approach you can just build the string, and then save it, rather than using multiple writes. You probably won't notice a difference though as long as the sv has decent I/O speeds. It really just depends on the system.

Also I suppose it's worth mentioning that you should save each player's data in a separate file with this approach, usually using the USGN ID as the file name is a good practice.

And as with most things that allow input, make sure you validate any data that the user is directly providing, to prevent hidden code (any potentially dangerous data would be in a string anyway though, so it would likely just return nil from the loadstring if something goes wrong). If you aren't saving anything that the user inputs then there is no concern.

Edit: One thing I'd like to mention. If file size is a concern I have a great example to put things in perspective. In my Rewards servers, all users are automatically registered if logged into USGN. Each user has 15 peices of data that is saved. I have a total of 10,484 registered users. The total file size, for all of the data, is just 2.7 MB. Since the data is in Lua tables, and I have a server management website that is written with CGILua, I can directly load and save the data when needed without any conversions. Whereas with PHP (or anything else) you would have to convert the data in some way on both sides (Lua script and Web). This further makes things more efficient and easier to manage (in my case).

Hope that at least gives some more ideas/options.
Happy Coding.
edited 1×, last 07.06.13 01:21:18 pm
To the start Previous 1 Next To the start
Log in to reply Scripts overviewCS2D overviewForums overview