Thalassicus
Bytes and Nibblers
How can I store data in saves, such as a table I'm using in lua?
-- Thank you to Afforess for providing his code open source. This save/load procedure is based off his work.
function doGameInitialization()
HappinessTable = {};
for iPlayerID,pPlayer in pairs(Players) do
HappinessTable[iPlayerID] = Queue:new();
if (isValidPlayer(pPlayer)) then
local SaveData = pPlayer:GetScriptData();
printDebug("Attempting to Load Save Data");
if SaveData ~= "" then
printDebug("Loading Save Data");
printDebug(SaveData);
for i=1, QUEUE_CAPACITY do
HappinessTable[iPlayerID]:push(0);
end
SaveData = stringToTable(SaveData, QUEUE_CAPACITY, 1, {","}, 1);
printContentsOfTable(SaveData);
HappinessTable[iPlayerID] = SaveData;
end
end
end
end
------------------------------------------------------------------------------
function saveGameData()
for iPlayerID,pPlayer in pairs(Players) do
if isValidPlayer(pPlayer) then
printDebug("Attempting to Save Data");
local tbl = tableToString(HappinessTable[iPlayerID], QUEUE_CAPACITY, 1, {","}, 1);
printDebug("Save Data Created");
printDebug(tbl);
pPlayer:SetScriptData(tbl);
end
end
end
------------------------------------------------------------------------------
function printContentsOfTable(incoming_table) -- For debugging purposes. LOT of table data being handled here.
printDebug("--------------------------------------------------");
printDebug("Table printout for table ID:", table);
for index, data in pairs(incoming_table) do
printDebug("Table index:", index, "Table entry:", data);
end
printDebug("- - - - - - - - - - - - - - - - - - - - - - - - - -");
end
function Queue:push( value )
table.insert(self, value);
if (self:size() >= QUEUE_CAPACITY) then
self:pop();
end
end
...
if not HappinessTable then
doGameInitialization();
end
updateHappinessInfo();
...
function updateHappinessInfo()
printDebug("updateHappinessInfo");
for iPlayerID,pPlayer in pairs(Players) do
if isValidPlayer(pPlayer) then
HappinessTable[iPlayerID]:push(pPlayer:GetExcessHappiness());
end
end
end
function printContentsOfTable(incoming_table) -- For debugging purposes. LOT of table data being handled here.
printDebug("--------------------------------------------------");
printDebug(string.format("Table printout for table ID: %d", table));
for index, data in pairs(incoming_table) do
printDebug(string.format("Table index: %d \t Table entry: %d", index, data));
end
printDebug("- - - - - - - - - - - - - - - - - - - - - - - - - -");
end
--
-- Queue class
--
Queue = { }
function Queue:new(o)
o = o or {};
setmetatable(o, self);
self.__index = self;
return o;
end
function Queue:push( value )
table.insert(self, value);
if (self:size() >= QUEUE_CAPACITY) then
self:pop();
end
end
function Queue:pop( )
if self:size() == 0 then
return nil;
end
local val = self[self.first];
table.remove(self, 1);
return val;
end
function Queue:size( )
return #self;
end
function Queue:average()
local sum = 0;
for id,val in pairs(self) do
--printDebug(self[i]);
sum = sum + val;
end
--printDebug("sum = " .. sum);
--printDebug("self:size() = " .. self:size());
return math.floor(sum / self:size());
end
function doGameInitialization()
HappinessTable = {};
for iPlayerID,pPlayer in pairs(Players) do
HappinessTable[iPlayerID] = Queue:new();
if (isValidPlayer(pPlayer)) then
local SaveData = pPlayer:GetScriptData();
printDebug("Attempting to Load Save Data");
if SaveData ~= "" then
printDebug("Loading Save Data");
printDebug(SaveData);
SaveData = stringToTable(SaveData, QUEUE_CAPACITY, 1, {","}, 1);
printContentsOfTable(SaveData);
HappinessTable[iPlayerID] = SaveData;
end
end
end
end
Mod: -21,-16,-7
Mod: --------------------------------------------------
Mod: Table printout for table ID:
Mod: Table index:
Mod: Table index:
Mod: Table index:
function doGameInitialization()
HappinessTable = {};
for iPlayerID,pPlayer in pairs(Players) do
> HappinessTable[iPlayerID] = Queue:new();
if (isValidPlayer(pPlayer)) then
local SaveData = pPlayer:GetScriptData();
printDebug("Attempting to Load Save Data");
if SaveData ~= "" then
printDebug("Loading Save Data");
printDebug(SaveData);
SaveData = stringToTable(SaveData, QUEUE_CAPACITY, 1, {","}, 1);
printContentsOfTable(SaveData);
> HappinessTable[iPlayerID] = SaveData;
end
end
end
end
-- Thank you to Afforess for providing his code open source. This save/load procedure is based off his work.
function doGameInitialization()
HappinessTable = {};
for iPlayerID,pPlayer in pairs(Players) do
if (isValidPlayer(pPlayer)) then
local SaveData = pPlayer:GetScriptData();
printDebug("Attempting to Load Save Data");
if SaveData == "" then
> HappinessTable[iPlayerID] = Queue:new();
else
printDebug("Loading Save Data");
printDebug(SaveData);
SaveData = stringToTable(SaveData, QUEUE_CAPACITY, 1, {","}, 1);
printContentsOfTable(SaveData);
> HappinessTable[iPlayerID] = Queue:new(SaveData);
end
end
end
end