Jump to content

Recommended Posts

I want to give my character a perk that allows her to instantly recruit pigs just by walking nearby. Though, I want to limit it to three at a time and have it happen when she's close to the pig. I tried to use the One Man Band's code to figure it out on my own, but couldn't figure it out. Mind giving me a hand?

Link to comment
https://forums.kleientertainment.com/forums/topic/87625-attracting-pigs/
Share on other sites

Here:

local master_postinit = function(inst)--reference on where it goes
        local function addpig(inst)
            local x,y,z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x,y,z, 5, {"pig"}, {"manrabbit"}, nil)
            for k,v in pairs(ents) do
                if v.components.follower and not v.components.follower.leader and not inst.components.leader:IsFollower(v) and inst.components.leader.numfollowers < 3 then
                    inst.components.leader:AddFollower(v)
                end
            end
end

inst:DoPeriodicTask(.5, addpig, nil, inst)

 

few notes to consider: the manrabbit is there to make sure you only make friends with pigs for free not bunnymen, 5 is the radius that makes pigs your friends edit it to your desired radius, the periodic task .5 is reasonable amount of time this activates. Last is this goes inside the character lua file.

@K1NGT1GER609

The code as is would make the game crash, due to the function not being closed off.

I tried to add an "end" between the last two ends, then the game crashes due to the inst not being declared.

It stopped crashing when I placed that "end" under the inst. But it doesn't seem to work.

20180215113949_1.jpg

As is? You didn't read the note on the master_postinit saying that its a reference on where the code goes and master_postinit should be in your character lua file already.

local function addpig(inst)
            local x,y,z = inst.Transform:GetWorldPosition()
            local ents = TheSim:FindEntities(x,y,z, 5, {"pig"}, {"manrabbit"}, nil)
            for k,v in pairs(ents) do
                if v.components.follower and not v.components.follower.leader and not inst.components.leader:IsFollower(v) and inst.components.leader.numfollowers < 3 then
                    inst.components.leader:AddFollower(v)
                end
            end
end

inst:DoPeriodicTask(.5, addpig, nil, inst)

There I removed the master_postinit make it a bit easier for you.

20180215120411_1.jpg

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