This is a read-only snapshot of the ComputerCraft forums, taken in April 2020.
LBPHacker's profile picture

Storing variables in a file

Started by LBPHacker, 22 February 2013 - 05:49 PM
LBPHacker #1
Posted 22 February 2013 - 06:49 PM
Say something… Hm…

Many have asked how to store variables in a file. And this is a basic tutorial about that. (You couldn't have been much more straightforward LBP, could you?)
This tutorial requires a basic knowledge of tables.

Step 1: Set up your variables inside a table

local health = 4
local hunger = 8
local armor = 3
local name = "Steve"

local target = {} -- this is a table
target.health = health
target.hunger = hunger
target.armor = armor
target.name = name

Step 2: Convert the table to a string

local output = textutils.serialize(target)

Step 3: Save the string to a file

local handle = assert(fs.open("vars", "w"), "Couldn't save vars") -- this will give you an error if something's gone wrong
handle.write(output) -- this is where the magic happens, we're storing the entire "target" table
handle.close()

We're done!
Now try to load that table.

Step 4: Read the serialized table

local handle = assert(fs.open("vars", "r"), "Couldn't load vars") -- this file is opened in read mode
local input = handle.readAll() -- input is now the serialized table
handle.close()

Step 5: Convert the string to a table

local destination = textutils.unserialize(input) -- this will decode our table

print(destination.health) -- prints 4
print(destination.hunger) -- prints 8
print(destination.armor) -- prints 3
print(destination.name) -- prints Steve

YAY! Now you can store variables in a file.

NOTE: You cannot store functions or "threads" (ok, coroutines) in files, because textutils doesn't like them. Of course, you could store a function dump, but I'm not sure what would happen to textutils then.
Bubba #2
Posted 22 February 2013 - 08:18 PM
NOTE: You cannot store variables or "threads" (ok, coroutines) in files, because textutils doesn't like them. Of course, you could store a function dump, but I'm not sure what would happen to textutils then.

To be more specific, textutils doesn't like anything isn't a number, boolean, string, or table (so long as the table is without recursive entries). You can store variables in the serialized table so long as they are one of those types.
theoriginalbit #3
Posted 23 February 2013 - 12:55 AM
The variables can be stored in files so long as they are of the same type.
Not true. They can be different types

-- our variables of different data types
local num = 5
local str = "Hi from string!"
local t = { "table", "data", 4 }
local bool = true

-- writing the above variables to the same file
local handle = fs.open("someFile", "w")
handle.write(tostring(num).."\n")
handle.write(str.."\n")
handle.write(textutils.serialize(t).."\n")
handle.write(tostring(bool).."\n")
handle.close()

-- reading those variables from the file
local handle = fs.open("someFile", "r")
num = tonumber( handle.readLine() )
str = handle.readLine()
t = textutils.unserialize( handle.readLine() )
bool = ( handle.readLine() == "true" )
handle.close()
LBPHacker #4
Posted 23 February 2013 - 06:55 AM
Oh crap, not
variables or "threads"
but FUNCTIONS and THREADS. Sorry…

EDIT: fixed