Miziak
Advanced User
- Joined
- Sep 16, 2008
- Messages
- 175
- Reaction score
- 27
Witajcie,
Zrobi?em jedn? fajn? funkcje dla waszych ots tym bardziej ogarni?ntym skrypterom na pewno si? przyda
Oto trzy funkcje kt?re u?atwi? nie jednej osobie ?ycie:
Ok, w data/lib/050-function.lua na ko?cu pliku wklejcie:
Nast?pnie w folderze data stw?rzcie plik: variables.lua i wklejcie do niego:
Zapiszcie pliki.
Przyk?ad u?ycia:
Pozdrawiam,
Miziak
Zrobi?em jedn? fajn? funkcje dla waszych ots tym bardziej ogarni?ntym skrypterom na pewno si? przyda
Oto trzy funkcje kt?re u?atwi? nie jednej osobie ?ycie:
- setPlayerStorageVariable(cid, virableId, virable) -- zapisuje w pliku konfiguracyjnym zmienne przypisan? do gracza mo?e ni? by? tablice, teksy, warto?ci logiczne(true/false), nic(nil), liczby
- getPlayerStorageVariable(cid, virableId) -- zwraca zmienn? o varableid przypisan? do gracza
- delPlayerStorageVariable(cid, virableId) -- usuwa zmienn? z pliku konfiguracyjnego
Ok, w data/lib/050-function.lua na ko?cu pliku wklejcie:
Code:
function convertTableToString(t)
local str = "{"
for i,v in pairs(t) do
str = str .. (type(i) == "number" and "[" .. i .. "]" or i) .. "=" .. (type(v) == "string" and "\"" .. v .. "\"" or v) .. ","
end
str = str:sub(1, str:len()-1) .. "}"
return str
end
function tableToString(t)
local str = "{"
for i,v in pairs(t) do
str = str .. (type(i) == "number" and "[" .. i .. "]" or i) .. "=" .. (type(v) == "table" and convertTableToString(v) or (type(v) == "string" and "\"" .. v .. "\"" or v)) .. ","
end
str = str:sub(1, str:len()-1) .. "}"
return str
end
function convertBoolToString(bool)
return (bool and "true" or "false")
end
dofile("data/variables.lua")
function setPlayerStorageVariable(cid, virableId, virable)
local sciezka = "data/variables.lua"
local linijki, lInt, bool = {}, 0, true
for line in io.lines(sciezka) do
table.insert(linijki, line)
lInt = lInt + 1
end
local file = assert(io.open(sciezka, "w"))
for i,_ in pairs(linijki) do
if(linijki[i]:find("name=\"" .. getCreatureName(cid) .. "\"") and linijki[i]:find("id=" .. virableId))then
local text = "{name=\"".. getCreatureName(cid) .."\", id=" .. virableId ..", value=" .. (type(virable) == "nil" and "nil" or (type(virable) == "boolean" and convertBoolToString(bool) or (type(virable) == "table" and tableToString(virable) or (type(virable) == "number" and virable or "\"" .. virable .. "\"" )))) .."},\n"
file:write(text)
bool = false
else
if(i == lInt - 1 and bool)then
local text = "{name=\"".. getCreatureName(cid) .."\", id=" .. virableId ..", value=" .. (type(virable) == "nil" and "nil" or (type(virable) == "boolean" and convertBoolToString(bool) or (type(virable) == "table" and tableToString(virable) or (type(virable) == "number" and virable or "\"" .. virable .. "\"" )))) .."},\n\n"
file:write(text)
else
file:write(linijki[i] .. (linijki[i] == "}" and "" or "\n"))
end
end
end
file:close()
end
function getPlayerStorageVariable(cid, virableId)
for _,v in pairs(variables) do
if(v.name == getCreatureName(cid) and v.id == virableId)then
return v.value
end
end
return -1
end
function delPlayerStorageVariable(cid, virableId)
local sciezka = "data/variables.lua"
local linijki = {}
for line in io.lines(sciezka) do
table.insert(linijki, line)
end
local file = assert(io.open(sciezka, "w"))
for i,_ in pairs(linijki) do
if(linijki[i]:find("name=\"" .. getCreatureName(cid) .. "\"") == nil or linijki[i]:find("id=" .. virableId) == nil)then
file:write(linijki[i] .. (linijki[i] == "}" and "" or "\n"))
end
end
file:close()
end
Nast?pnie w folderze data stw?rzcie plik: variables.lua i wklejcie do niego:
Code:
variables = {
}
Zapiszcie pliki.
Przyk?ad u?ycia:
Code:
function onSay(cid, words, param, channel)
if(words == "set")then
setPlayerStorageVariable(cid, 1, {a=1, [2]="s", b=false, [6]= {"I'm Polish!!!"}})
elseif(words == "get")then
local v = getPlayerStorageVariable(cid, 1)
print("OMG!! this is " .. type(v) .. "!")
for i,v in pairs(v) do
print(i .. " == " .. v)
end
end
return true
end
Pozdrawiam,
Miziak
Notka moderatorska: |
Dobre - przenosz? do dzia?u g??wnego. |