👫Behavior inheritance

Principles

Behavior inheritance is very useful when you wish to extend an existing behavior and add more capabilities on top of it.

A great example of this is the NACT_Patrol behavior. This behavior inherits from NACT_Detection behavior. NACT_Detection will try to search for an enemy and progressivly raise its suspicions if an enemy is in sight. Patrolling, is all of the above but the NPC will also circle around a route instead of standing still.

Constructor

While declaring the NACT_Patrol global, you must make sure it Inherits from the behavior you wish to extend

NACT_Patrol = NACT_Detection.Inherit("NACT_Patrol")

The constructor is similar to any orther behavior

function NACT_Patrol:Constructor(NpcInstance, tBehaviorConfig)
    self:Super().Constructor(self, NpcInstance, tBehaviorConfig)
end

The key difference there is that we call the Parent class constructor too!

Extended functions

For NACT_Patrol, we don't need to override the Main class, this is still a possibility if you need to change or extend the Main function, however, we need to walk to a next point

function NACT_Patrol:WalkToNextPoint()
    self.npc:MoveToPoint(self.patrolPoints[self.targetPatrolPointIndex])
    if (self.targetPatrolPointIndex == self.maxPatrolPointIndex) then
        if (self.patrolRoute.walkMethod == "circle") then
            self.targetPatrolPointIndex = 1
        else
            Console.Warn ("Circling method not implemented")
        end
    else 
        self.targetPatrolPointIndex = self.targetPatrolPointIndex + 1
    end
end

Using Event delegation we wait until the NPC has reached the point we assigned listening to the MoveComplete event and scheduling when we will start moving towards the next point.

function NACT_Patrol:OnMoveComplete()
    Timer.SetTimeout(function()
        self:WalkToNextPoint()
    end, math.random(self.waitForMin, self.waitForMax))
end

Destructor

Always remember to clean ! We have not created new timers or significantly changed the NPC modes, however, the parent behavior has, remember to call it's destructor!

function NACT_Cover:Destructor()
    if (self.nearestCoverPoint) then
        self:LeaveCover()
    end
    Timer.ClearInterval(self.timerMain)
end

Last updated