• There is NO official Otland's Discord server and NO official Otland's server list. The Otland's Staff does not manage any Discord server or server list. Moderators or administrator of any Discord server or server lists have NO connection to the Otland's Staff. Do not get scammed!
  • New resources must be posted under Resources tab. A discussion thread will be created automatically, you can't open threads manually anymore.

Storing Tables in Storages (setGlobalTableStorage)

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:



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
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
Code:
setPlayerTableStorage(cid, 17271, {["level"] = getPlayerLevel(cid), ["sex"] = getPlayerSex(cid) == 1 and "female" or "male"})
Will work just fine.


But
Code:
setPlayerTableStorage(cid, 17271, {returnDouble = function(value) return value*2 end, try = 11, [2] = 10})
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:
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"})
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.
 
Last edited:
Cool. I have developed something like this, but more powerful. I have used a lib to translate tables to strings and then to transform it to table again i have used dostring function. It was more powerful because with this lib i can store tables inside tables, boolean values etc, but as you know it was not efficient at all. I will make a test to see if its more efficient or not.
 
Well, the good thing about my lib is it transforms the string into table and vice versa 'manually'. So you won't have any problems with your indexes and variables relations as they will stay exactly the way you wrote them.
 
Back
Top