๐Ÿค”Decision behavior

What is a Decision behavior?

As you know each behavior is responsible to switch to anorther behavior in NACT, however sometimes, this can lead to a lot of code repetition. A decision behavior is a type of behavior wich sole purpose is to take a decision and immediatly switch to anorther

NACT_Combat in details

Combat is the main decision behavior of the default millitary NPCs. When the following behaviors:

  • NACT_Engage

  • NACT_Seek

  • NACT_Cover

  • NACT_Alert

have finished, they will always go back to NACT_Combat for a decision.

For example in NACT_Engage:

    if (self.npc:ShouldReload()) then
        self.npc:SetBehavior(self.mainBehavior)
        return
    end
    
    if (not bFocusedVisible) then
        self.npc:SetBehavior(self.mainBehavior)
    end

    if (self:TimeElapsed() > self.maxTimeEngaged) then
        self.npc:SetBehavior(self.mainBehavior)
    end

With the mainBehavior being NACT_Combatby default

Constructor

Like any orther behavior, a decision behavior have the invoker NPC as the first parameter and the config provided when spawning the NPC.

function NACT_Combat:Constructor(NpcInstance, tBehaviorConfig)
    self.npc = NpcInstance
 
    self.timeoutHandle = Timer.SetTimeout(function()
        self:Main()
    end)
end

This time, we will not use an interval but a Timeout, without any delay (meaning processing in the next tick). We need to be sure the class is correctly instancied before, easiest way is calling the Main function

Main function

Unlike orther behavior, the only think the decision behavior should take care of

function NACT_Combat:Main()
    local rng = math.random(0, self.rngMax)
    if (self.npc:ShouldReload() or rng == self.rngCoverValue) then
        self.npc:SetBehavior(self.coverBehavior)
        return
    else
        if (#self.npc.territory:GetEnemiesInZone() == 0) then
             self.npc:SetBehavior(self.idleBehavior)
             return
        end
        if (self.npc:IsFocusedVisible() == false) then
            self.npc:SetBehavior(self.seekBehavior)
            return
        else
            self.npc:TurnToFocused()
            self.npc:SetBehavior(self.attackBehavior)
            return
        end
    end
end

Like any behavior, remember to clean behind you !

function NACT_Combat:Destructor()
    Timer.ClearTimeout(self.timeoutHandle)
end

Last updated