Lighter save system

This commit is contained in:
theo@manjaro 2023-06-07 17:09:59 +02:00
parent da29df87d2
commit a8a0d6a7a7
4 changed files with 47 additions and 6 deletions

View File

@ -1,12 +1,16 @@
local modname = "DeadGod Helper"
local mod = RegisterMod(modname, 1)
local json = require("json")
local math = require("math")
mod.print = Isaac.ConsoleOutput
mod.game = Game()
local save = include("save"):new()
if mod:HasData() then
save = json.decode(mod:LoadData())
save:load(mod:LoadData())
local c = 0
for i,v in ipairs(save.seen) do
if v then c = c+1 end
end
mod.print(modname.." Loaded, "..c.." items seen\n")
end
mod.font = Font()
@ -30,7 +34,8 @@ function mod.registerTouched(id)
end
-- Save
mod:SaveData(json.encode(save))
mod:RemoveData()
mod:SaveData(save:dump())
end
-- Used to check if EID's description should be modified
@ -64,10 +69,10 @@ function mod:update()
if player:GetCollectibleNum(data._deadgodlastid)>data._deadgodlastcount then -- Have it on me
-- Then I just picked it up, yay
mod.registerTouched(data._deadgodlastid)
mod.print("> "..tostring(data._deadgodlastid).."\n")
data._deadgodlastid = 0
data._deadgodlastcount = 0
end
end
end end
end
if item and item:IsCollectible() then
data._deadgodlastid = item.ID
@ -88,6 +93,13 @@ function mod:PickupDrawCallback(pickupEntity, _)
end
end
function mod:preGameExit()
mod:RemoveData()
mod:SaveData(save:dump())
end
mod:AddCallback(ModCallbacks.MC_PRE_GAME_EXIT, mod.preGameExit)
mod:AddCallback(ModCallbacks.MC_POST_UPDATE, mod.update)
mod:AddCallback(ModCallbacks.MC_POST_PICKUP_RENDER, mod.PickupDrawCallback)

View File

@ -4,7 +4,7 @@
<directory>deadgod helper</directory>
<id>2985754961</id>
<description></description>
<version>1.1</version>
<version>1.3</version>
<visibility>Private</visibility>
<tag id="Items"/>
<tag id="Graphics"/>

View File

@ -19,6 +19,35 @@ function savegen:new()
self.seen[id] = true
end
function save:dump()
-- Returns a string representation of the save
-- Bool to symbol
local f = function(x) return (x and "1") or "0" end
local result = f(self.settings.eid)..f(self.settings.visual)..f(self.settings.showonblind)
for i=1, save.settings.maxid do
result = result..f(save.seen[i])
end
return result
end
function save:load(text)
-- Change attributes based on the text
-- Symbol to bool
local f = function(x, i)
return x:sub(i,i)=="1"
end
if (#text>=3) then
-- First 3 are the settings
self.settings.eid = f(text,1)
self.settings.visual = f(text,2)
self.settings.showonblind = f(text,3)
end
local offset = 3
for id=1, self.settings.maxid do
self.seen[id] = f(text,offset+id)
end
end
return save
end

Binary file not shown.

Before

Width:  |  Height:  |  Size: 8.2 KiB

After

Width:  |  Height:  |  Size: 10 KiB