Jump to content

Recommended Posts

Sorry about all the follower-related threads. Just can't seem to resolve this problem. :wilson_cry:

Anyway, as I've mentioned before, my character (same one) has a problem with his code that works with followers. This time (and hopefully the last time), it has to do with garden plants. Since he has a One-man Band-type ability, him doing work such as digging, etc. near plants should count as talking to them but it instead causes a crash. Here is the log:

server_log_plants.txt

And, for the sake of convenience and hoping the problem can be fixed, I'll paste just the follower code here too:

This is the function for it:

local function OnWorked(inst, data)
local x,y,z = inst.Transform:GetWorldPosition() -- Get the player's location
local ents = TheSim:FindEntities(x,y,z, TUNING.ONEMANBAND_RANGE, nil, {"werepig"}, {"pig", "merm", "farm_plant", "player"}) -- Find things near the player's location
    for k,target in pairs(ents) do -- For each valid found entity run the following code
        if target:HasTag("player") or target.components.follower:GetLeader() == inst then -- If the target is a player or a creature that follows you
            target:AddDebuff("buff_kk", "buff_kk") -- apply the buff
        elseif target.components.follower and not target.components.follower.leader and not inst.components.leader:IsFollower(target) and inst.components.leader.numfollowers < 10 then
            if target:HasTag("merm") then
                if target:HasTag("mermguard") then
                    if inst:HasTag("merm") and not inst:HasTag("mermdisguise") then
                        inst.components.leader:AddFollower(target)
                    end
                else
                    if inst:HasTag("merm") or (TheWorld.components.mermkingmanager and TheWorld.components.mermkingmanager:HasKing()) then
                        inst.components.leader:AddFollower(target)
                    end
                end
            else
                inst.components.leader:AddFollower(target)
            end

        elseif target.components.farmplanttendable ~= nil then
            target.components.farmplanttendable:TendTo(inst)
        end
    end
end

This is in the master_postinit:

    inst.onFollowerDeath = function(follower) -- create a function and add it to the character
        if inst.prefab == "kk" then -- Double check that the leader is playing KK, there should be no case where this can fail, but just in-case
            inst.components.sanity:DoDelta(-15) --Remove 15 sanity
			print ("inst.onFollowerDeath")
		end
    end
    inst.onFollowerLeave = function(follower) -- create a function and add it to the character
        follower:RemoveEventCallback("death", inst.onFollowerDeath) --Remove our ListenForEvent so we don't lose sanity from past-followers dieing
        follower:RemoveEventCallback("stopfollowing", inst.onFollowerLeave) --Same idea here
    end
    if inst.components.leader then --Make sure we are not messing with something that doesn't exist (For custom gamemodes and such)
        local oldAddFollower = inst.components.leader.AddFollower -- Store a backup of the AddFollower function for later use
        inst.components.leader.AddFollower = function(self, follower) -- Replace the AddFollower function with our own
            oldAddFollower(self, follower) -- Run the stored backup of the AddFollower function so we don't miss out on the original functionality of it
            follower:ListenForEvent("death", inst.onFollowerDeath) --Run the "onFollowerDeath" function when the follower dies
            follower:ListenForEvent("stopfollowing", inst.onFollowerLeave) --Run the "onFollowerLeave" function when the follower stops following us
        end
    end

And this is the buff function itself for lowering K_K's own and other players' hunger drain in range while working:

local function OnAttached(inst, target)
	if target and target.components.hunger ~= nil then -- Check if the target has the hunger component so we don't try messing with something that doesn't exist
		target.components.hunger.burnratemodifiers:SetModifier(inst, 0.1)
		print("Activate Hunger Drain Buff")
	end
end

local function OnDetached(inst, target)

end

local function OnExtended(inst, target)
	inst.components.timer:SetTimeLeft("buffover", 5)
end

local function OnTimerDone(inst, data)
    if data.name == "buffover" then
        inst.components.debuff:Stop()
			inst:Remove()
    end
end

local function buff_fn()
        local inst = CreateEntity()

        if not TheWorld.ismastersim then
            --Not meant for client!
            inst:DoTaskInTime(0, inst.Remove)
            return inst
        end

        inst.entity:AddTransform()

        --[[Non-networked entity]]
        --inst.entity:SetCanSleep(false)
        inst.entity:Hide()
        inst.persists = false

        inst:AddTag("CLASSIFIED")

        inst:AddComponent("debuff")
        inst.components.debuff:SetAttachedFn(OnAttached)
        inst.components.debuff:SetDetachedFn(OnDetached)
        inst.components.debuff:SetExtendedFn(OnExtended)
        inst.components.debuff.keepondespawn = true

        inst:AddComponent("timer")
        inst.components.timer:StartTimer("buffover", 5)
        inst:ListenForEvent("timerdone", OnTimerDone)

        return inst
end

Thank you for any help!

The problem is that you don't have a check to see if the entity has a follower component:

if target:HasTag("player") or (target.components.follower and target.components.follower:GetLeader() == inst) then -- If the target is a player or a creature that follows you

This should fix your error.

For your other problems I can't unfortunaly not really help you, the targeting problem is probably due to how pigs target their opponents. You will need to have a look at the game files and may need to alter the retargetfn or similar.

The double sanity reduction looks good, but if you don't receive a print, something is wrong there. Try to add more print statements along the way to see where it fails :)

  • GL Happy 1

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