Skip to content

bootstrap SaveModule SP #71

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 2 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
--contributors: Reifel
SaveModule = SaveModule or {}

function SaveModule:new(modID, save, load)
local o = {}
setmetatable(o, self)
self.__index = self
o.modID = modID
o.save = self.save
o.load = self.load
o.clear = self.clear
o.custom_save = save
o.custom_load = load
o.modDataTable = ModData.getOrCreate(modID)
return o
end

function SaveModule:load()
if self.modDataTable.keep == nil then return end
self.custom_load(self.modDataTable.keep)
end

function SaveModule:save()
self.modDataTable.keep = self.custom_save()
end

function SaveModule:clear()
self.modDataTable = {}
end

--possible future improvements, not implemented yet MP compatibility
--FOR MP CAN USE ModData https://discord.com/channels/136501320340209664/232196827577974784/995661819408551947
--Monkey lib can be useful too for MP Monkey_ModData at MonkeysLibrary
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
require "SaveModule"

local modID = "YOUR_MOD_ID" --maybe there is a way to automatic grab the current file Mod ID

local save = function()
--OWN LOGIC
local mod_data = {
--persistentVarName1= GlobalVar.Name1
}
return mod_data
end

local load = function(mod_data)
--OWN LOGIC
for k,v in pairs(mod_data.persistentVarName1) do
print(k)
print(v)
end
--GlobalVar.Name1 = mod_data.persistentVarName1
end

GlobalVar_load = function()
GlobalVar.save_instance = SaveModule:new(modID, save, load)
GlobalVar.save_instance:load()
end

GlobalVar_save = function()
GlobalVar.save_instance:save()
end
Events.OnGameStart.Add(GlobalVar_load)
Events.OnSave.Add(GlobalVar_save)

--you can use inside a code at any point too, not only events


--if dont want to use module, and without usage of embed MP compatibility (future), do like this
--local mysave = ModData.getOrCreate(modID)
--if mysave.keep then
-- load(mysave.keep)
-- print(mysave)
--end

--local mysave = ModData.getOrCreate(modID)
--mysave.keep = save()
--print(mysave)