Dateien

> > CS2D > Lua Scripts > [SALT] Save and Load Tables
DateiübersichtCS2D-ÜbersichtLua Scripts-Übersicht

Englisch [SALT] Save and Load Tables >

13 Kommentare3 kb, 582 Downloads

alt [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:
Mehr >


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
Mehr >
2× editiert, zuletzt 16.08.17 05:04:58
Zugelassen von Starkkz

Download Download

3 kb, 582 Downloads

Kommentare

13 Kommentare
Zum Anfang Vorherige 1 Nächste Zum Anfang

Logge dich ein!

Du musst dich einloggen, um selber Kommentare verfassen zu können!Einloggen

alt

Ajmin
User Off Offline

I like it because i love it.
Ich mag es!

alt

Hajt
User Off Offline

Pretty fast method of saving/loading data.
Ich mag es!

alt

Gaios
Reviewer Off Offline

nice, thanks
Ich mag es!

alt

saschalois
User Off Offline

Nice Stuff
Ich mag es!

alt

Masea
Super User Off Offline

What a good guy...
Ich mag es!

alt

THEMUD
User Off Offline

Awesome! Very useful!
Ich mag es!

alt

Inflexion
User Off Offline

Finaally, something that could be useful.
Ich mag es!

alt

TopNotch
User Off Offline

This is just great.
Ich mag es!

alt

The Smoker
User Off Offline

Very useful script
Ich mag es!

alt

tontonEd
User Off Offline

very underrated file, it's save a lot of time thanks.
Ich mag es!

alt

Yates
Reviewer Off Offline

Thaannkk you.
Ich mag es!

alt

MikuAuahDark
User Off Offline

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

alt

_Yank
User Off Offline

Although I'm not using this, this is a pretty good method to save and load stuff.
Ich mag es!
Zum Anfang Vorherige 1 Nächste Zum Anfang