Jump to content

Recommended Posts

I've looked at several examples of modifications to brains and I thought I had it sorted, but I'm still tearing my hair out to properly understand it.

 

I use this method via AddBrainPostInit to find my node of interest:

local actiontoreplace = nillocal actiontoreplaceindex = nil    for i,node in ipairs(brain.bt.root.children) do    if node.name == "RunAway" then        actiontoreplace = node        actiontoreplaceindex = i        print("RunAway found in a SmallBirdBrain!")        break    endend    if not actiontoreplace then    print("Couldn't find the 'RunAway' behaviour in smallbird brain!")    returnendtable.remove(brain.bt.root.children, actiontoreplaceindex)print("RunAway removed from smallbird brain!")actiontoreplace.shouldrunfn = function() return ShouldRunAwayFromPlayer(brain.inst) endtable.insert(brain.bt.root.children, actiontoreplaceindex, actiontoreplace)print("RunAway re-added to smallbird brain!")

.

This method spams my ShouldRunAwayFromPlayer function within my mod when I get near the bird, as I had hoped and the bird does not run away. Now I set myself to be the leader of the bird, or set the bird to have me as its leader and whenever I get too close, the bird Runs Away.

 

I really don't quite understand what I am doing here evidently, as even if I comment out the table.insert and just remove RunAway completely, making the bird my follower instantly makes it RunAway when too close, even though I can print a list of the nodes at that point and see there is no RunAway any more, it still does it!

 

As a workaround, I manually edited the smallbirdbrain.lua file and tweaked the MIN_FOLLOW_DIST, MAX_FOLLOW_DIST, TARGET_FOLLOW_DIST, START_RUN_DIST and STOP_RUN_DIST to achieve the effect I wanted, but I can't even work out how to instead override those values from my mod.. I feel really pants on head here, can anyone help please? :-)

@outseeker
Check out the shouldrunfn parameters in the smallbird brain. The function itself receives a target and the ShouldRunAwayFromPlayer receives the brain.inst and the target.
Since you aren't modyfing all the node itself but rather the function, there is no need to remove and re-insert it.

AddBrainPostInit("smallbirdbrain", function(brain)    local function CustomRunAway(inst, target, oldfn)        if target and target.prefab == "wolfgang" then            return true        else            return oldfn(inst,target)        end    end        local index = nil        for i,v in ipairs(brain.bt.root.children) do        if v.name == "RunAway" then            index = i            break        end    end        if not index then        return    end      local old_shouldrunfn = brain.bt.root.children[index].shouldrunfn   brain.bt.root.children[index].shouldrunfn = function(target) return CustomRunAway(brain.inst, target, old_shouldrunfn) endend)

The above is working just fine for me. Smallbird always run away when I'm playing as wolfgang, and have the old run away behavior when I'm playing as other character. This aproach is quite API friendly too, since we are saving the old function and then defaulting to it in case the target isn't the character we want to work with.

 

If you want to edit the follow distances, you'll need to nest some for cycles or hope that the top Follow node is still in brain.bt.root.children[3].children[2].children[2].children[2]

@Ryuushu, thanks heaps for this code!

 

Initially it looked perfect, but ultimately I still have the exact same problem! The bird keeps running away once I am designated its leader, even when I Think I am manually overriding any possible RunAway.. Check this out :<

 

AddBrainPostInit("smallbirdbrain", function(brain)     local function CustomRunAway(inst, target, oldfn)        if target and target:HasTag("character") then			if not inst.components.follower.leader then				inst.components.follower:SetLeader(target)			end            return false -- Do not run away.        else            return false -- Do not *$# run away. oldfn(inst,target)        end    end         local index = nil         for i,v in ipairs(brain.bt.root.children) do        if v.name == "RunAway" then            index = i            break        end    end         if not index then        return    end       local old_shouldrunfn = brain.bt.root.children[index].shouldrunfn   brain.bt.root.children[index].shouldrunfn = function(target) return CustomRunAway(brain.inst, target, old_shouldrunfn) endend)

Totally just your perfectly working code, with one line added to make the player leader and no restriction of having to be Wolfgang. OK and the final desperate change to no longer call back to the original RunAway under any circumstance. No longer stops them running away! lol waiiiiii XD

 

*EDIT* OK, so I think it's actually not RunAway that causes them to run away >.<

The Follow has a min, max and target follow distance that causes them to move away when closer than the minimum.. So my workaround to edit those values is required, which is good and bad news.

 

Good news is you are spot on and the Follow node of the bt is where you specified, bad news is I still don't exactly understand the method to modify those min/max values set initially in smallbirdbrain.lua

Edited by outseeker
target:HasTag("character")

I think you want :HasTag("player") 

 

Edit: "character" is also applied to players, but to many other things as well -- abigail, bunnymen, chester, merms, walrus, smallbirds, etc

Edited by rezecib

@rezecib, thanks mate. From what I've seen in code it is indeed applied to players (and so I used it XD) but "player" would be more correct, you're absolutely right.

 

Am I understanding correctly that in order to change the TARGET_FOLLOW_DIST, etc. set at the top of smallbirdbrain.lua, I will actually need to use the above method of modifying the functions themselves for each of the 3 Follow nodes found in the bt? :< I just need to change some basic configuration strings lol

 

I can't properly translate the Follow functionality from the brain into mod-context either it seems, where these values are read and used. I need a manual! >.<

Trying to make some progress.. I have managed to identify all 3 Follow nodes in the smallbirdbrain but I still don't understand how to rewrite the original function to work in mod context, or if I can go about changing these few settings any other way.

 

The Follow Node functions in the smallbirdbrain.lua all look like this:

Follow(self.inst, function() return self.inst.components.follower.leader end, MIN_FOLLOW_DIST, TARGET_FOLLOW_DIST, MAX_FOLLOW_DIST)

and the Follow Nodes themselves I can interact with via my mod using:

brain.bt.root.children[3].children[2].children[2].children[2].shouldrunfn =brain.bt.root.children[6].children[2].children[2].children[2].shouldrunfn =brain.bt.root.children[7].children[2].shouldrunfn =

The problem is, I don't understand how to translate the former to the latter in order to set the shouldrunfn on these Follow nodes. I've tried just muddling through, pasting various combinations and changing things but nothing has worked for me thus far.

Surely there's an easier way to set those _DIST's than all this editing?? XD

OK, I am somewhat at a loss now.

 

Whenever I try writing the value of a Follow shouldrunfn, I can't make it work like Ryuushu's example for RunAway.

 

I can see what portion of the original RunAway function was used in the fine example provided, but using the same approach on Follow nodes doesn't seem to yield the same result. In fact, I can't even make it run my CustomFollow() function when it's formatted like shouldrunfn = function() etc. return - it just doesn't run my code and doesn't stop the Follow functionality either. I can make it run my code by saying shouldrunfn = CustomFollow() but this doesn't stop followers from following me. I don't get it XD

 

Any pointers please anyone? I would really appreciate it, as I'm pretty much done with my mod apart from the inability to control the follower distances. I gave it a configuration and all the nifty things, I'm just stuck at my own understanding/logic hurdles.

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