Files

> > CS2D > Lua Scripts > [SALT] Save and Load Tables
Files overviewCS2D overviewLua Scripts overview

English [SALT] Save and Load Tables >

13 comments3 kb, 622 Downloads

old [SALT] Save and Load Tables

VaiN
User Off Offline

I've seen many posts about people struggling to save and load data, and using all kinds of strange ways to go about it.

This is just a couple of helpful functions to make saving and loading much easier.

How does it work?
It simply allows you to save and load a table of data. The table can also include sub-tables.

For example, maybe you have a table of player data you want to save:
More >


What Lua data types are supported?
√ Booleans (true/false)
√ Strings (quotes are escaped for you)
√ Numbers
√ Sub-Tables of the above types
× functions
× userdata

Why just functions and not a complete user/admin/whatever script?
I realize there are plenty of complete systems out there for saving and such. Many of them have a specific way they think data, ranks, permissions, or whatever should be handled. This is intended to allow you to make your own system, or use it with an existing one. You can also use this outside of CS2D as it's just plain Lua. For example I use this externally to load reports and save them to a database so my community can manage them on the web.

How is this any better than X method?
It's faster for Lua to parse code using the loadfile() function than it is to use the io functions and parse data yourself. Being able to save recursively allows you to organize your data into sub-tables however you like and keep things clean. It's human-readable and indented, so it's easy to read or modify if needed.

Other Notes:
The salt.save() function writes data as it goes so that it can process a LOT of data if needed, making it more efficient and allowing it to support sub-tables without wasted memory. As long as the saved data returns a Lua table, it can be loaded with salt.load().

Because of how it writes as it goes, it will save unsupported types as nil. Keep this in mind if you are using OOP for your data and plan ahead. I have no intention of adding support for functions and I know it's possible.

I've made the code easy to read, but if you have any questions just ask. Let me know if you run into any issues. It's been tested but only in practical and intended uses.

Update 2017-08-15:
Source Code
The source code is now available at Github.

Changes
The functions are no longer stealing the `table` namespace, and instead follows the Lua standard for modules.

Installation
Installation is possible via LuaRocks (recommended):
1
luarocks install salt

or via git
1
git clone https://github.com/VaiN474/salt.git

Or download the zip here and extract it.

Then, either copy salt.lua to the directory your scripts are loaded from (i.e. sys/lua/) or add the path salt.lua is saved in to your package.path.

Example Usage
1
2
3
salt = require("salt")
salt.save(my_table,"/path/to/file")
my_table,err = salt.load("/path/to/file")

License
I've added a proper license, using the same license as Lua itself - MIT
More >
edited 2×, last 16.08.17 05:04:58 am
Approved by Starkkz

Download Download

3 kb, 622 Downloads

Comments

13 comments
To the start Previous 1 Next To the start

Log in!

You need to log in to be able to write comments!Log in

old

Ajmin
User Off Offline

I like it because i love it.
I like it!

old

Hajt
User Off Offline

Pretty fast method of saving/loading data.
I like it!

old

Gaios
Reviewer Off Offline

nice, thanks
I like it!

old

saschalois
User Off Offline

Nice Stuff
I like it!

old

Masea
Super User Off Offline

What a good guy...
I like it!

old

THEMUD
User Off Offline

Awesome! Very useful!
I like it!

old

Inflexion
User Off Offline

Finaally, something that could be useful.
I like it!

old

TopNotch
User Off Offline

This is just great.
I like it!

old

The Smoker
User Off Offline

Very useful script
I like it!

old

tontonEd
User Off Offline

very underrated file, it's save a lot of time thanks.
I like it!

old

Yates
Reviewer Off Offline

Thaannkk you.
I like it!

old

MikuAuahDark
User Off Offline

I already did something similar before, with exception that it doesn't save, it returns string instead(like uneval does)

old

_Yank
User Off Offline

Although I'm not using this, this is a pretty good method to save and load stuff.
I like it!
To the start Previous 1 Next To the start