Jump to content

Recommended Posts

So I've made a fair bit of progress with a mod I'm making with a friend of mine and we're looking to give our character the ability to summon and befriend ghosts. Currently ghosts are neutral to her and she can summon them by crafting a custom item we've made for her. I was even able to make it so that the ghosts where summoned with loyalty and that they'd recognise the player who summoned them as their leader, however I did have to overwrite the ghost's prefab with a modified version to do so. But even though I can assign them loyalty and make them into followers they still don't behave like followers, I'm assuming that this is because their "ghostbrain" script isn't coded to account for the ghost being a follower. I have made a few attempts to fix this but it seems overwriting the ghosts brain has ended up crashing my game. I tried looking over the brain scripts for entities that do follow the character (pigmen and Abigail) however I couldn't seem to identify what parts of their scripts where responsible for making them follow the player.

AddBrainPostInit(function (brain)
--code
end)

is to alter brains without using the brains if you didn't know.

require "behaviours/follow"--behavouirs or like little functions
require "behaviours/wander"
require "behaviours/faceentity"
require "behaviours/panic"


local MIN_FOLLOW_DIST = 0--distances for how close and how far you can go
local MAX_FOLLOW_DIST = 12
local TARGET_FOLLOW_DIST = 6

local MAX_WANDER_DIST = 3


local function GetFaceTargetFn(inst) -- :) idk
    return inst.components.follower.leader
end

local function KeepFaceTargetFn(inst, target) -- if the target is the leader
    return inst.components.follower.leader == target
end


local ChesterBrain = Class(Brain, function(self, inst) --brAIn stuff
    Brain._ctor(self, inst)
end)


function ChesterBrain:OnStart()
    local root = 
    PriorityNode({
        --WHILE: you have this PANIC
        WhileNode( function() return self.inst.components.hauntable and self.inst.components.hauntable.panic end, "PanicHaunted", Panic(self.inst)),
        WhileNode( function() return self.inst.components.health.takingfiredamage end, "OnFire", Panic(self.inst)),

        --idk follow if leader is there
        Follow(self.inst, function() return self.inst.components.follower.leader end, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST),
        --this basically makes it face you
        FaceEntity(self.inst, GetFaceTargetFn, KeepFaceTargetFn),
        --this is just a cute little wander if he has a home 
        Wander(self.inst, function() return self.inst.components.knownlocations:GetLocation("home") end, MAX_WANDER_DIST),
        
    }, .25)
    self.bt = BT(self.inst, root)
end

return ChesterBrain

 

3 hours ago, PyrotechnicCake said:

however I couldn't seem to identify what parts of their scripts where responsible for making them follow the player.

This is the code from chester since he only follows and stuff :)

local RETARGET_MUST_TAGS = { "_combat" }
local RETARGET_CANT_TAGS = { "playerghost", "INLIMBO" }
local function retargetfn(inst)
    --Find things attacking leader
    local leader = inst.components.follower:GetLeader()
    return leader ~= nil
        and FindEntity(
            leader,
            TUNING.SHADOWWAXWELL_TARGET_DIST,
            function(guy)
                return guy ~= inst
                    and (guy.components.combat:TargetIs(leader) or
                        guy.components.combat:TargetIs(inst))
                    and inst.components.combat:CanTarget(guy)
            end,
            RETARGET_MUST_TAGS, -- see entityreplica.lua
            RETARGET_CANT_TAGS
        )
        or nil
end

inst.components.combat:SetRetargetFunction(2, retargetfn)

:)

ok so after trying to implement this I couldn't get the game to start, I'm not sure if I'm putting it in the right place or if I'm making some other mistake but when I try adding the code it doesn't work. Is it supposed to be in the ghost prefab? and even so, I'm not sure where abouts to put it. Atm I'm only trying to add this:

AddBrainPostInit(function (brain)
--code
end)

Am I going about this the wrong way? I'm trying to add it without any extra functions first to try and eliminate any potential errors.

Yeah so every time I try to add the

AddBrainPostInit(function (brain)
--code
end)

part of the code it ends up crashing the game when I try to launch. I'm not sure what I'm doing wrong here, I've searched for other uses of AddBrainPostInit but I couldn't figure out why it wasn't working. What am I doing wrong?

Well, first I had to add the follower related stuff to the ghost's prefab

    --Allows ghosts to be tamed
    inst:AddComponent("follower")
    inst.components.follower.maxfollowtime = TUNING.PIG_LOYALTY_MAXTIME
    inst.components.follower:SetLeader(nil)

then I had to call a function in the ghost brain to actually check for the leader

    local function GetLeader(inst)
        return inst.components.follower.leader
    end

And then I edited the code a bit so that they'd actually follow their leader

function GhostBrain:OnStart()
    local root = PriorityNode(
    {
        WhileNode(function() return GetLeader(self.inst) ~= nil end, "FollowTarget", 
            Follow(self.inst, GetLeader, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST)
        ),
        WhileNode(function() return GetFollowTarget(self.inst) ~= nil end, "FollowTarget", 
            Follow(self.inst, function() return self.inst.brain.followtarget end, TUNING.GHOST_RADIUS*.25, TUNING.GHOST_RADIUS*.5, TUNING.GHOST_RADIUS)
        ),
        SequenceNode{
            ParallelNodeAny{
                WaitNode(10),
                Wander(self.inst),
            },
            ActionNode(function() self.inst.sg:GoToState("dissipate") end),
        }
    }, 1)
        
    self.bt = BT(self.inst, root)
end

the first node is the one that handles the following, not sure if this is the cleanest way though.

A current issue with this method though is they seem to want to prioritize keeping outside their owner's minimum follow distance over fighting their target in combat, this makes them very poor allies, and given that their stats are very low and they have a chance to just up and vanish on you currently, so unless I figure out how to fix that the character is going to be a very sub-par version of Wendy.

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