Jump to content

Saving/Loading Data?

Recommended Posts

Don't see many mods that use this, but I think I need to.  I'm taming hounds, and all the code works, but when I reload the game, the hounds forget who they're supposed to be following.

>>>This lets hounds be trained, but should also call the save and load functions:
local function AddHoundPostInit(fn)
    AddPrefabPostInitAny( function(inst)
        if inst and inst:HasTag("hound") and not inst:HasTag("clay") then  --Only non-clay hounds can be trained.
            inst.OnSave = onsave  --Test code.
            inst.OnLoad = onload

>>>And this is my current save/load code.  I think I know what to save and load, but haven't yet figured out how to actually, do, that..:

local function onsave(inst, data)  --Test code.
--    if inst:HasTag("burnt") or (inst.components.burnable ~= nil and inst.components.burnable:IsBurning()) then  --Code from tent.
--        data.burnt = true
--    end

--    inst.components.follower and inst.components.follower.leader and inst.components.follower.leader:HasTag("houndtamer")  --Values I need to save?
--    self.targettime = currentTime + timeLeft  --targettime seems to control hound's loyalty clock.

    if inst.components.follower.leader then
        data.leader = inst.components.follower.leader  --Store leaders name?

local function onload(inst, data)  --Test code.
--    if data ~= nil and data.burnt then  --Code from tent.
--        inst.components.burnable.onburnt(inst)
--    end

    if data ~= nil and data.leader then
        inst.components.follower.leader(inst)  --Set old leader as new leader.

Edited by FurryEskimo
Link to comment
Share on other sites

Haven't made much progress.  The default friendship level (for pigmen) is saved like this:

function Friendlevels:OnSave()
    local taskscomplete = {}
    for i,task in ipairs(self.friendlytasks)do
        table.insert(taskscomplete,{complete = task.complete})        
    return {enabled = self.enabled, level = self.level, taskscomplete = taskscomplete, queuedrewards = self.queuedrewards}

function Friendlevels:OnLoad(data)
    self.enabled = data.enabled
    self.level = data.level   
    self.queuedrewards = data.queuedrewards or {}
    if #self.queuedrewards > 0 then

function Friendlevels:LoadPostPass(newents, data)
    if data ~= nil and data.taskscomplete ~= nil then
        for i,task in ipairs(self.friendlytasks) do
            if data.taskscomplete then
                task.complete = data.taskscomplete.complete

>>>But I'm using a unique value that I haven't figured out how to save yet.:

-- Should add loyalty in a way that will not conflict with other mods.
    local follower = inst.components.follower
    local AddLoyaltyTime_prev = follower.AddLoyaltyTime
    function follower:AddLoyaltyTime(time)
        if follower.leader and follower.leader:HasTag("houndtamer") then
            local currentTime = GLOBAL.GetTime()
            local timeLeft = self.targettime or 0
            timeLeft = math.max(0, timeLeft - currentTime)
            timeLeft = math.min(HOUND_LOYALTY_MAXTIME or 0, timeLeft + time)
            self.targettime = currentTime + timeLeft

            if self.task then
                self.task = nil
            self.task = self.inst:DoTaskInTime(timeLeft, stopfollow)
            return AddLoyaltyTime_prev(self, time)


    local onaccept_prev = inst.components.trader.onaccept
    local onaccept_new = function(inst, giver, item)
        if not giver:HasTag("houndtamer") then
            -- Do whatever function it was before
            if onaccept_prev then
                return onaccept_prev(inst, giver, item)
        if inst.components.eater:CanEat(item) then  

            local playedfriendsfx = false
            if inst.components.combat.target and inst.components.combat.target == giver then
            elseif giver.components.leader then
                playedfriendsfx = true
                local loyaltyTime = item.components.edible:GetHunger() * HOUND_LOYALTY_PER_HUNGER
            --    if inst.component.health.current < 150 then  --Test code.  Hounds heal when fed.  Note: Causes the mod to break.
            --        inst.components.health.current == inst.components.health.current + 10
            --    end


Edited by FurryEskimo
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...