Jump to content

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.
            fn(inst)
            inst.OnSave = onsave  --Test code.
            inst.OnLoad = onload
        end
    end)
end
 

>>>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?
    end
end

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.
    end
end

Edited by FurryEskimo
Link to comment
https://forums.kleientertainment.com/forums/topic/121343-savingloading-data/
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})        
    end
    return {enabled = self.enabled, level = self.level, taskscomplete = taskscomplete, queuedrewards = self.queuedrewards}
end

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

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
            end
        end
    end
end
 --------------------------------------------------------------------------------------------------------------------------------------

>>>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:Cancel()
                self.task = nil
            end
            self.task = self.inst:DoTaskInTime(timeLeft, stopfollow)
        else
            return AddLoyaltyTime_prev(self, time)
        end
    end

-------------------------------------------------------------------

    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)
            end
            return
        end
        if inst.components.eater:CanEat(item) then  

            local playedfriendsfx = false
            if inst.components.combat.target and inst.components.combat.target == giver then
                inst.components.combat:SetTarget(nil)
            elseif giver.components.leader then
                inst.SoundEmitter:PlaySound("dontstarve/common/makeFriend")
                playedfriendsfx = true
                giver.components.leader:AddFollower(inst)
                local loyaltyTime = item.components.edible:GetHunger() * HOUND_LOYALTY_PER_HUNGER
                inst.components.follower:AddLoyaltyTime(loyaltyTime)
            --    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
            end

        end
    end

Edited by FurryEskimo

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...