π Serialization Guide
July 25, 2025 Β· View on GitHub
π Quick Start Guide for serializing and deserializing Lua variables with LuaDoTheWorld
π What You'll Learn
- β How to serialize Lua tables to strings
- β How to deserialize strings back to Lua objects
- β How to save serialized data to files
- β How to load serialized data from files
π οΈ Prerequisites
- LuaDoTheWorld installed and required in your script
π¦ Serialize a Lua Table
Convert any Lua table to a string that can be saved or transmitted:
local dtw = require("luaDoTheWorld/luaDoTheWorld")
local data_to_serialize = {
name = "mateusΓããã",
age = 30,
hobbies = {"coding", "gaming"}
}
local serialized = dtw.serialize_var(data_to_serialize)
print(serialized)
-- Output: (function();return {['age'] = 30.000000, ['hobbies'] = {'coding', 'gaming' }, ['name'] = 'mateus\195\130\195\163\195\163\195\163' };end)();
πΎ Save Serialized Data to File
Store your serialized data in a file for later use:
local dtw = require("luaDoTheWorld/luaDoTheWorld")
local data = {
name = "JoΓ£o",
score = 95.5,
items = {"sword", "shield", "potion"}
}
local serialized = dtw.serialize_var(data)
local formatted = "return " .. serialized
dtw.write_file("player_data.lua", formatted)
π Load Serialized Data from File
Retrieve your data back using Lua's require system:
-- Load using require (this also evaluates/executes the code)
local player_data = require("player_data")
print(player_data.name) -- JoΓ£o
print(player_data.score) -- 95.5
π Deserialize from String
Convert a serialized string back to a Lua object:
local dtw = require("luaDoTheWorld/luaDoTheWorld")
-- Get serialized data
local data = {level = 5, coins = 1000}
local serialized = dtw.serialize_var(data)
-- Deserialize back to object (evaluates the code!)
local restored_data = dtw.interpret_serialized_var(serialized)
print(restored_data.level) -- 5
print(restored_data.coins) -- 1000
β οΈ Safety Tips
- π Both methods execute code -
require()andinterpret_serialized_var()both evaluate Lua code - π‘οΈ Only use with trusted data - Never load serialized data from untrusted sources
- πΎ Save as
.luafiles - This allows you to use Lua's require system - π§Ή Add "return " prefix - When saving to files for require compatibility
- π Prefer
require()for files - It's the standard way to load Lua modules
π Quick Reference
| Function | What it does | Example |
|---|---|---|
dtw.serialize_var(table) | Convert table to string | dtw.serialize_var({a=1, b=2}) |
dtw.interpret_serialized_var(str) | Evaluate string as Lua code | dtw.interpret_serialized_var(serialized) |
dtw.write_file(path, data) | Save string to file | dtw.write_file("data.lua", content) |
require("filename") | Load and evaluate .lua file | require("data") |
π― Complete Example
Here's a full workflow from serialization to loading:
local dtw = require("luaDoTheWorld/luaDoTheWorld")
-- 1. Create some data
local game_state = {
player = "Alice",
level = 10,
inventory = {"key", "map", "torch"},
stats = {
health = 100,
mana = 50
}
}
-- 2. Serialize the data
local serialized = dtw.serialize_var(game_state)
-- 3. Save to file (add "return " for require compatibility)
local formatted = "return " .. serialized
dtw.write_file("game_save.lua", formatted)
-- 4. Load the data back
local loaded_game = require("game_save")
print("Welcome back, " .. loaded_game.player .. "!")
print("You are at level " .. loaded_game.level)
π What Gets Serialized?
β Supported Types:
- Numbers (integers and floats)
- Strings (including UTF-8)
- Tables (nested tables supported)
- Booleans
β Not Supported:
- Functions
- Userdata
- Threads
- Circular references
π Need Help?
- π Check the main SDK documentation
- π Look at other example scripts in the SDK
- π Report issues on our GitHub repository