vitorbertolucci
Member
- Joined
- Apr 15, 2014
- Messages
- 75
- Reaction score
- 18
Introduction:
Some days ago I was creating a system and felt the need of storing several information (player by player) in only one place. However, we all know its not possible to store a table inside a storage. For this reason, I decided to make this storing possible.
I'm sure this library is going to be an extremely useful tool for all scripters. It opens many doors and will make the creation of many interesting systems possible.
Instalation:
Just create the file tableStorage.lua in data/lib and put this code:
Usage:
To store a table in a player's storage, use:
Example:
To store a table in a global storage, use
Example:
To return the tables, use:
or
Clarification:
The tables returned by the function getGlobalTableStorage and getPlayerTableStorage are normal tables and can be manipulated as any other, for example:
Observations:
Theese tables wont store boolean values, other tables nor functions, only strings and numbers. Therefore, they can absolutely receive values from functions, for example
Will work just fine.
But
Won't work.
The tables can be filled with or without defined index, so that the not defined indexes will be automatically defined in a row (Like in any other table). Thus:
Are all valid.
Be careful with too big tables. They can end up consuming a lot of your database memory. I advise you not to exceed 4 elements per table. Of course only one very big table won't cause any trouble but, if you have a lot of players, it may be dangerous to add many big tables (1 per player).
Considerations:
I really think this library will open several doors to all of you, scripters. And I hope you create many nice systems using it. If you have any doubts or suggestions, I'm here to answer.
Cya.
Some days ago I was creating a system and felt the need of storing several information (player by player) in only one place. However, we all know its not possible to store a table inside a storage. For this reason, I decided to make this storing possible.
I'm sure this library is going to be an extremely useful tool for all scripters. It opens many doors and will make the creation of many interesting systems possible.
Instalation:
Just create the file tableStorage.lua in data/lib and put this code:
Code:
-- Functions library created by Vitor Bertolucci (Killua)
-- Functions used to store tables in storages
killua_functions = {
filtrateString = function(str) -- By Killua
local tb, x, old, last = {}, 0, 0, 0
local first, second, final = 0, 0, 0
if type(str) ~= "string" then
return tb
end
for i = 2, #str-1 do
if string.byte(str:sub(i,i)) == string.byte(':') then
x, second, last = x+1, i-1, i+2
for t = last,#str-1 do
if string.byte(str:sub(t,t)) == string.byte(',') then
first = x == 1 and 2 or old
old, final = t+2, t-1
local index, var = str:sub(first,second), str:sub(last,final)
tb[tonumber(index) or tostring(index)] = tonumber(var) or tostring(var)
break
end
end
end
end
return tb
end,
translateIntoString = function(tb) -- By Killua
local str = ""
if type(tb) ~= "table" then
return str
end
for i, t in pairs(tb) do
str = str..i..": "..t..", "
end
str = "a"..str.."a"
return tostring(str)
end
}
function setPlayerTableStorage(cid, key, value)
return doPlayerSetStorageValue(cid, key, killua_functions.translateIntoString(value))
end
function getPlayerTableStorage(cid, key)
return killua_functions.filtrateString(getPlayerStorageValue(cid, key))
end
function setGlobalTableStorage(key, value)
return setGlobalStorageValue(key, killua_functions.translateIntoString(value))
end
function getGlobalTableStorage(key)
return killua_functions.filtrateString(getGlobalStorageValue(key))
end
Usage:
To store a table in a player's storage, use:
Code:
setPlayerTableStorage(cid, key, tabela)
Example:
Code:
setPlayerTableStorage(cid, 199991, {["first"] = 4, [8] = "Killua", [3] = 22})
To store a table in a global storage, use
Code:
setGlobalTableStorage(key, tabela)
Example:
Code:
setGlobalStorageValue(123412, {1, 55, "c", 3})
To return the tables, use:
Code:
getGlobalTableStorage(key)
or
Code:
getPlayerTableStorage(cid, key)
Clarification:
The tables returned by the function getGlobalTableStorage and getPlayerTableStorage are normal tables and can be manipulated as any other, for example:
Code:
for t, k in pairs(getPlayerTableStorage(cid, 123444)) do
print("["..t.."] "..v)
end
Theese tables wont store boolean values, other tables nor functions, only strings and numbers. Therefore, they can absolutely receive values from functions, for example
Code:
setPlayerTableStorage(cid, 17271, {["level"] = getPlayerLevel(cid), ["sex"] = getPlayerSex(cid) == 1 and "female" or "male"})
But
Code:
setPlayerTableStorage(cid, 17271, {returnDouble = function(value) return value*2 end, try = 11, [2] = 10})
The tables can be filled with or without defined index, so that the not defined indexes will be automatically defined in a row (Like in any other table). Thus:
Code:
setPlayerTableStorage(cid, 12344, {1,3,23,1999,"test"})
setPlayerTableStorage(cid, 12344, {[5] = "hi", ["mia"] = 32, c = 18})
setPlayerTableStorage(cid, 12344, {1, "c", 6, ["test"] = 18, b = "c", "hello world"})
Be careful with too big tables. They can end up consuming a lot of your database memory. I advise you not to exceed 4 elements per table. Of course only one very big table won't cause any trouble but, if you have a lot of players, it may be dangerous to add many big tables (1 per player).
Considerations:
I really think this library will open several doors to all of you, scripters. And I hope you create many nice systems using it. If you have any doubts or suggestions, I'm here to answer.
Cya.
Last edited: