You only do it when shutting down or starting up, so no players are online anyway.Saving whole tables is really really good. The bad thing is performance.
You only do it when shutting down or starting up, so no players are online anyway.Saving whole tables is really really good. The bad thing is performance.
If well executed it has far better performence then a database query.Saving whole tables is really really good. The bad thing is performance.
My load/save idea consists more of a chunk loading type, we only load the chunks which we actually use, so we don't buffer up the ram usage to much.You only do it when shutting down or starting up, so no players are online anyway.
TFS 1.0 is much different than 0.3.6 when it comes to storages.What's the problem with storages? I made a Pet System (some yeard ago, for TFS 0.3.6 i think) and all the info was saved with players storage (pet exp, mana/health, level) and you just have to create a lib containing all pet info (i.e: spells, exprate, etc)
Oh sorry. I was thinking in using it in Player( Its mean loading on login and saving on logout ).You only do it when shutting down or starting up, so no players are online anyway.
What's the problem with storages? I made a Pet System (some yeard ago, for TFS 0.3.6 i think) and all the info was saved with players storage (pet exp, mana/health, level) and you just have to create a lib containing all pet info (i.e: spells, exprate, etc)
Summ it looks neat, but can you give example how to use it and how to execute it.Code:function table.serialize(t, depth) local depth = depth or 0 local ret, first = '{\n', true for k, v in pairs(t) do if not first then ret = ret .. ',\n' end ret = ret .. (' '):rep(depth + 1) if type(k) == 'number' then ret = ret .. '[' .. k .. '] = ' elseif type(k) == 'string' then ret = ret .. '["' .. tostring(k):gsub('"', '\\"') .. '"] = ' else error('Unhandled key type') end if type(v) == 'number' then ret = ret .. v elseif type(v) == 'string' then ret = ret .. v:gsub('"', '\\"') elseif type(v) == 'table' then ret = ret .. table.serialize(v, depth + 1) elseif type(v) == 'boolean' or type(v) == 'nil' then ret = ret .. (v == true and 'true' or v == false and 'false' or 'nil') else error('Unhandled value type') end first = false end ret = ret .. '\n' .. (' '):rep(depth) .. '}' return ret end function table.unserialize(str) return loadstring('return ' .. str)() end
In case you wanna save your globalstorages or another table.
TFS 1.0 is much different than 0.3.6 when it comes to storages.
Oh sorry. I was thinking in using it in Player( Its mean loading on login and saving on logout ).
Storages on tfs 1.0 only accepts integer as keys and values. Very bad.
STORAGE_BASE = 1000
PET_EXP = STORAGE_BASE + 1
PET_HEALTH = STORAGE_BASE + 2
.
.
.
I do not see a reason why you would need a ssd for such a case, we don't talk about writing 200k lines in a file each execute, so this happens in a blink of an eye for a normal hdd aswell.answer is ssd or caching last used files?
answer is ssd or caching last used files?Saving whole tables is really really good. The bad thing is performance.
pet = {}
function table.serialize(t, depth) -- credits Summ, I've just tweaked it some.
local depth = depth or 0
local ret, first = '\n' .. (' '):rep(depth) .. '{\n', true
for k, v in pairs(t) do
if not first then
ret = ret .. ',\n'
end
ret = ret .. (' '):rep(depth + 1)
if type(k) == 'number' then
ret = ret .. '[' .. k .. '] = '
elseif type(k) == 'string' and t[k] ~= nil then
ret = ret .. '["' .. tostring(k):gsub('"', '\\"') .. '"] = '
else
error('Unhandled key type')
end
if type(v) == 'number' then
ret = ret .. v
elseif type(v) == 'string' then
ret = ret .. '"'.. v ..'"'
elseif type(v) == 'table' then
ret = ret .. table.serialize(v, depth + 1)
elseif type(v) == 'boolean' or type(v) == 'nil' then
ret = ret .. (v == true and 'true' or v == false and 'false' or 'nil')
else
error('Unhandled value type')
end
first = false
end
ret = ret .. '\n' .. (' '):rep(depth) .. '}'
return ret
end
function Player.savePets(self)
local file = io.open("data/pets/".. self:getName() ..".lua", "w")
file:write('pet["'.. self:getName() ..'"] =' .. table.serialize(pet[self:getName()]))
file:close()
end
function Player.loadPets(self)
dofile("data/pets/".. self:getName() ..".lua")
end
function Player.createPet(self, name, outfit, healthMax, manaMax, spells, petType)
if pet[self:getName()] ~= nil then
if pet[self:getName()][name] ~= nil then
return self:sendCancelMessage("You already have a pet with that name.")
end
else
pet[self:getName()] = {}
end
pet[self:getName()][name] = {}
pet[self:getName()][name].outfit = outfit
pet[self:getName()][name].healthMax = healthMax
pet[self:getName()][name].health = healthMax
pet[self:getName()][name].manaMax = manaMax
pet[self:getName()][name].mana = manaMax
pet[self:getName()][name].level = 1
pet[self:getName()][name].experience = 0
pet[self:getName()][name].petType = petType
pet[self:getName()][name].spells = spells
pet[self:getName()][name].backpack = {}
self:savePets()
end
Here's the first progress contains creating/saving and loading the pets.
the picture above just shows an example of how the creation looks like when it's beeing saved in the lua file.
needs to go into global.lua (if someone feels like testing it )
You just need to create a folder called "pets" in the data folder.Code:pet = {} function table.serialize(t, depth) -- credits Summ, I've just tweaked it some. local depth = depth or 0 local ret, first = '\n' .. (' '):rep(depth) .. '{\n', true for k, v in pairs(t) do if not first then ret = ret .. ',\n' end ret = ret .. (' '):rep(depth + 1) if type(k) == 'number' then ret = ret .. '[' .. k .. '] = ' elseif type(k) == 'string' and t[k] ~= nil then ret = ret .. '["' .. tostring(k):gsub('"', '\\"') .. '"] = ' else error('Unhandled key type') end if type(v) == 'number' then ret = ret .. v elseif type(v) == 'string' then ret = ret .. '"'.. v ..'"' elseif type(v) == 'table' then ret = ret .. table.serialize(v, depth + 1) elseif type(v) == 'boolean' or type(v) == 'nil' then ret = ret .. (v == true and 'true' or v == false and 'false' or 'nil') else error('Unhandled value type') end first = false end ret = ret .. '\n' .. (' '):rep(depth) .. '}' return ret end function Player.savePets(self) local file = io.open("data/pets/".. self:getName() ..".lua", "w") file:write('pet["'.. self:getName() ..'"] =' .. table.serialize(pet[self:getName()])) file:close() end function Player.loadPets(self) dofile("data/pets/".. self:getName() ..".lua") end function Player.createPet(self, name, outfit, healthMax, manaMax, spells, petType) if pet[self:getName()] ~= nil then if pet[self:getName()][name] ~= nil then return self:sendCancelMessage("You already have a pet with that name.") end else pet[self:getName()] = {} end pet[self:getName()][name] = {} pet[self:getName()][name].outfit = outfit pet[self:getName()][name].healthMax = healthMax pet[self:getName()][name].health = healthMax pet[self:getName()][name].manaMax = manaMax pet[self:getName()][name].mana = manaMax pet[self:getName()][name].level = 1 pet[self:getName()][name].experience = 0 pet[self:getName()][name].petType = petType pet[self:getName()][name].spells = spells pet[self:getName()][name].backpack = {} self:savePets() end
Now I can get myself into the core and create everything around it
function Player.savePets(self)
local playerName = self:getName()
local file = io.open("data/pets/".. playerName ..".lua", "w")
file:write('pet["'.. playerName ..'"] =' .. table.serialize(pet[playerName]))
file:close()
end
function Player.createPet(self, name, outfit, healthMax, manaMax, spells, petType)
local playerName = self:getName()
if not pet[playerName] then
pet[playerName] = {}
end
if pet[playerName][name] then
return self:sendCancelMessage("You already have a pet with that name.")
end
pet[playerName][name] = {}
pet[playerName][name].outfit = outfit
pet[playerName][name].healthMax = healthMax
pet[playerName][name].health = healthMax
pet[playerName][name].manaMax = manaMax
pet[playerName][name].mana = manaMax
pet[playerName][name].level = 1
pet[playerName][name].experience = 0
pet[playerName][name].petType = petType
pet[playerName][name].spells = spells
pet[playerName][name].backpack = {}
self:savePets()
end
pet[player:getName()][petname].say = {}
pet[player:getName()][petname].say[1] = "I rip them apart"
pet[player:getName()][petname].say[2] = "You are my bro"