Jump to content

Recommended Posts

Hello, I'm currently working on some brain code, and I've been having some trouble and would appreciate some insight. I'm looking to code an instance to walk to a pre-defined coordinate (x, y, z), but don't know what behavior I should/can use.

Most of the code I've found (like the leash behavior) focuses on making an instance walk towards another instance, rather than a specific coordinate.

I'd appreciate any help you can offer, thanks!

1 hour ago, _zwb said:

Maybe you can use inst.components.locomotor:GoToPoint(Point(x,y,z)) ?

Yeah, I figure it's something like that, but I haven't figured out how to code that yet in the brain.
 

function Traveler_Brain:OnStart()
    local root =
        PriorityNode(
        {
            --Check if the character should panic.
            WhileNode(function() return self.inst.components.hauntable and self.inst.components.hauntable.panic end, "PanicHaunted", 
                Panic(self.inst)
            ),

            WhileNode(function() return self.inst:HasTag("onopen") end, "StandStill",
                StandStill(self.inst)
            ),

            --Check if the character should travel.
            WhileNode(function() return self.inst.Traveler_Stats.LevelData.TravelCoordinates ~= nil end, "ShouldTravel",
                SequenceNode{
                    --Travel to the destination.
                    DoAction(self.inst, function() return GoToPoint(self.inst, self.inst.Traveler_Stats.LevelData.TravelCoordinates) end, "Travel To Destination"),
                    --Wander until traveltime is zero or less.
                    WhileNode(function() return self.inst.traveltime > 0 end, "Wander Until TravelTime Ends",
                        Wander(self.inst, function() return self.inst.Traveler_Stats.LevelData.TravelCoordinates end, MAX_WANDER_DIST)
                    ),
                }
            ),
            
            --Check if the character should go home.
            WhileNode(function() return ShouldGoHome(self.inst) end, "ShouldGoHome",
                DoAction(self.inst, function() return GoHomeAction(self.inst) end, "go home", true)
            ),
            
            FaceEntity(self.inst, GetFaceTargetFn, KeepFaceTargetFn),
            
            --More behaviors as needed.
        }, .25)

    self.bt = BT(self.inst, root)
end

 

OMG, I THINK I GOT IT!!

This took me Hours, but I think I got it!!!!
Is it perfect?  No, absolutely not, but this was so irksome, I'm so happy, caused like five different files to crash just to get this far.  Wooooo~

If you know how to further improve this, please say something.  (Turns out part of the issue was generating a coordinate and storing it in a custom array, rather than inst.pt, which is important for, some reason.)

require "behaviours/doaction"
require "behaviours/wander"
require "behaviours/standstill"

local MAX_WANDER_DIST = 5  --Adjust this distance as needed.  --Note: Add wander/stand still/function to return home.

local TravelerBrain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)

function TravelerBrain:OnStart()

    local root =
        PriorityNode(
        {

        --Traveling.
        WhileNode(function() return self.inst.components.locomotor and self.inst.pt end, "",
            ChattyNode(self.inst, {""},
                ActionNode(function ( ... )
                --    print("Traveling.")
                 --   print(self.pt)
                    self.inst.components.locomotor:GoToPoint(self.pt, nil, false)
                end))),

            --Add more behaviors here as needed.
        }, .25)

    self.bt = BT(self.inst, root)
end

return TravelerBrain

 

  • GL Happy 1

Hmm, there's seems to be a downside to this.  I can see that they're only traveling while near the player.  Once they get around two screens away they halt.  Perhaps there's a way to ensure the game will continue to sim them?  There are lots of mods that have AI followers you leave at a location, does anyone know how they work?  (I haven't used many of those mods, so I haven't reviewed their code.)

Edited by FurryEskimo

Some general improvements to this code I figured out.  Turns out the instance was walking, but only to (0, 0, 0).
Inside the instance:

inst.pt = Vector3(x, y, z)  --Create a Vector3 object for the location.
inst.components.knownlocations:RememberLocation("Destination_1", inst_pt, false)

Inside the brain:

require "behaviours/doaction"
require "behaviours/wander"
require "behaviours/standstill"

local MAX_WANDER_DIST = 5  --Adjust this distance as needed.  --Note: Add wander/stand still/function to return home.

local TravelerBrain = Class(Brain, function(self, inst)
    Brain._ctor(self, inst)
end)

function TravelerBrain:OnStart()
	local destination = self.inst.components.knownlocations:GetLocation("Destination_1")

    local root =
        PriorityNode(
        {

        --Traveling.
        WhileNode(function() return self.inst.components.locomotor and destination end, "",
            ChattyNode(self.inst, {""},
                ActionNode(function ( ... )
                    self.inst.components.locomotor:GoToPoint(destination, nil, false)
                end))),

            --Add more behaviors here as needed.
        }, .25)

    self.bt = BT(self.inst, root)
end

return TravelerBrain

This still isn't perfect, but it took me all day to figure out I needed to use the Vector3 code.  Hope this is helpful to someone.

 

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