How to save a table to a string and convert it back to a table

Post Reply
LC Creations
Member
Posts: 159
Joined: Mon Feb 18, 2019 02:53
GitHub: LandonAConway

How to save a table to a string and convert it back to a table

by LC Creations » Post

I need to save an entire table to a string then convert it back to a table when the server is opened up.
cdb_ac3a146dcafb

User avatar
Andrey01
Member
Posts: 2579
Joined: Wed Oct 19, 2016 15:18
GitHub: Andrey2470T
In-game: Andrey01
Location: Russia, Moscow

Re: How to save a table to a string and convert it back to a table

by Andrey01 » Post

To convert a table to the string, use minetest.serialize()

To convert back, use minetest.deserialize()

https://github.com/minetest/minetest/bl ... .txt#L5414

User avatar
FreeLikeGNU
Member
Posts: 280
Joined: Tue Oct 28, 2014 02:50
GitHub: FreeLikeGNU
IRC: freelikegnu
In-game: FreeLikeGNU

Re: How to save a table to a string and convert it back to a table

by FreeLikeGNU » Post

Now if only we had a helper to convert a couch to a string and back...

ShadMOrdre
Member
Posts: 1118
Joined: Mon Dec 29, 2014 08:07
Location: USA

Re: How to save a table to a string and convert it back to a table

by ShadMOrdre » Post

look at my lib_towns mod on github. or lib_materials or lib_ecology. or the newly released lib_mg_continental. The last one is the best example.

I generate random positions and save the values to both a table and a string. The string builds, record by record, an indexed table, complete with .csv style seperators. If properly constructed, you can unpack each record right back into a table a startup.

The meat of the code is from Gael de Sailly. There are many implementations of this as a quick and easy database in the above mentioned mods.

Post Reply

Who is online

Users browsing this forum: No registered users and 8 guests