πŸ”„ Serialization Guide

July 25, 2025 Β· View on GitHub

Lua API Difficulty Version

πŸš€ 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() and interpret_serialized_var() both evaluate Lua code
  • πŸ›‘οΈ Only use with trusted data - Never load serialized data from untrusted sources
  • πŸ’Ύ Save as .lua files - 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

FunctionWhat it doesExample
dtw.serialize_var(table)Convert table to stringdtw.serialize_var({a=1, b=2})
dtw.interpret_serialized_var(str)Evaluate string as Lua codedtw.interpret_serialized_var(serialized)
dtw.write_file(path, data)Save string to filedtw.write_file("data.lua", content)
require("filename")Load and evaluate .lua filerequire("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

Footer