This is a new API, by me. It allows you to create variables, that can be used even after the program is exited, or the computer is rebooted. It saves the variables to the disk, and loads them when calling the variable.
NEWS!!!: Now supports tables! though not tableception :o/>/>
To create a table variable, use this code(With your own table ofc:D):
Spoiler
var.create('test', 'table')
var.setValue('test', {1, 'Hello', 'World!', true, false, nil})
Functions
Spoiler
These are the current functions, and I'm planning to add more in future updates
var.create(name, type) --Creates a variable
var.delete(name) --Deletes a variable
var.exists(name) --Checks if a variable exists
var.getType(name) --Returns the type of the variable
var.getValue(name) --Returns the value of the variable
var.setType(name, type) --Sets the type of the variable(deletes the value)
var.setValue(name, value) --Sets the type of the variable(If the type is correct)
Examples
Spoiler
Creating a variable, setting the value and printing the type and value
Spoiler
var.create('test', 'number') --Creating the variable test
var.setValue('test', 13) --Setting the value of test to 13
local sType = var.getType('test')
local value = var.getValue('test')
print(sType..' '..value)
----------------------------------------
Output:
number 13
Changing the type of a variable, and printing it(Using the variable from before)
Spoiler
var.setType('test', 'string')
var.setValue('test', 'I am a string')
local sType = var.getType('test')
local value = var.getValue('test')
print(sType..' '..value)
----------------------------------------
Output:
string I am a string
Removing a variable(Using the variable from before)
Spoiler
var.delete('test')
Code
Spoiler
--Copyright (C) 2012 Mads Clausen, Tomoyo Apps http://www.tomoyo.uphero.com
function exists(name)
if fs.exists('vars/'..name..'.txt') then
return true
end
end
function getType(name)
if exists(name) then
local file = io.open('vars/'..name..'.txt', 'r')
local line = file:read('*l')
file:close()
return line
else
print('var.getType(): Variable does not exist')
error()
end
end
function getValue(name)
if exists(name) then
if getType(name) ~= 'table' then
local file = io.open('vars/'..name..'.txt', 'r')
local line = file:read('*l')
line = file:read('*l')
file:close()
if getType(name) == 'number' then
return tonumber(line)
elseif getType(name) == 'string' then
return line
elseif getType(name) == 'boolean' then
if line == 'true' then
return true
elseif line == 'false' then
return false
end
elseif getType(name) == 'nil' then
return nil
end
else
local file = io.open('vars/'..name..'.txt', 'r')
local line = file:read('*l')
local lines = {}
local returnTable = {}
local i = 1
line = file:read('*l')
while line ~= nil do
lines[i] = line
line = file:read('*l')
i = i + 1
end
for _i = 1, #lines do
local pos = string.find(lines[_i], '_')
local _type = string.sub(lines[_i], 1, pos - 1)
local value = string.sub(lines[_i], pos + 1)
if _type == 'number' then
returnTable[_i] = tonumber(value)
elseif _type == 'string' then
returnTable[_i] = value
elseif _type == 'boolean' then
if value == 'true' then
returnTable[_i] = true
elseif value == 'false' thenW
returnTable[_i] = false
end
elseif _type == 'nil' then
returnTable[_i] = nil
elseif _type == 'table' then
print('var.getValue(): You cannot have a table inside a table')
end
end
return returnTable
end
else
print('var.getValue(): Variable does not exist')
error()
end
end
function setType(name, _type)
if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
print('var.setType(): Unsupported type, or the variable already exists')
error()
else
local file = io.open('vars/'..name..'.txt', 'w')
file:write(_type)
file:close()
end
end
function setValue(name, value)
if exists(name) then
if tostring(type(value)) ~= getType(name) then
print('var.setValue(): Wrong type: '..type(value))
error()
end
if getType(name) ~= 'table' then
local file = io.open('vars/'..name..'.txt', 'r')
local firstLine = file:read('*l')
file:close()
local file = io.open('vars/'..name..'.txt', 'w')
file:write(firstLine..'n'..tostring(value))
file:close()
else
local file = io.open('vars/'..name..'.txt', 'r')
local firstLine = file:read('*l')
file:close()
local file = io.open('vars/'..name..'.txt', 'w')
file:write(firstLine..'n')
for i = 1, #value do
file:write(type(value[i])..'_'..tostring(value[i])..' n')
end
file:close()
end
else
print('var.setValue(): Variable does not exist')
error()
end
end
function create(name, _type)
if not fs.exists('vars') then fs.makeDir('vars') end
if string.lower(_type) ~= 'number' and string.lower(_type) ~= 'string' and string.lower(_type) ~= 'boolean' and string.lower(_type) ~= 'nil' and string.lower(_type) ~= 'table' and exists(name) then
print('var.create(): Unsupported type, or the variable already exists')
error()
else
local file = io.open('vars/'..name..'.txt', 'w')
file:write(_type)
file:close()
end
end
function delete(name)
if exists(name) then
fs.delete('vars/'..name..'.txt')
else
print('var.delete(): Variable does not exist')
error()
end
end
Please leave some feedback, so I know what to improve. Enjoy!