- 1 Description
- 2 Tasks
- 3 Engine Tasks
- 4 Custom Tasks
NPC behavior should be handled mostly through schedules. A schedule represents the root-node of a collection of tasks, which build a behavior tree.
You can add new tasks to the schedule, which will be executed one by one in order. Each task can have multiple sub-tasks.
A task is an atomic activity a NPC can perform. Complex behavior can be created by binding tasks together through a schedule.
You can create custom tasks in Lua, however there are also some pre-defined hardcoded tasks available.
Some tasks can have additional parameters assigned to them, to change the task behavior dynamically. There are two ways of doing so:
If you want a task to always use a specific parameter value, you can use one of the AIBehaviorTask:SetTask*-functions.
You can use AIBehaviorTask:SetScheduleParameter to make a task-parameter point to a schedule parameter instead. This is useful if you have multiple tasks using the same parameter.
local sched = ai.create_schedule() sched:SetParameterEntity(0,ents.get_players()) -- Schedule parameter 0 local taskMove = ai.create_task(ai.TASK_MOVE_TO_TARGET) taskMove:SetScheduleParameter(0,0) -- Task parameter 0 will point to schedule parameter 0 sched:AddTask(taskMove) local taskActivity = ai.create_task(ai.TASK_PLAY_ACTIVITY) taskActivity:SetParameterInt(0,ACT_MELEE_ATTACK1) taskActivity:SetScheduleParameter(1,0) -- Task parameter 1 will point to schedule parameter 0 sched:AddTask(taskActivity)
This schedule will make the NPC move to player 1, and play an attack animation while facing player 1.
There are several pre-defined tasks which you can use for generic behavior. These are hardcoded and cannot be modified (Aside from parameters) or derived from:
The NPC will move to the specified target. The task will succeed once the NPC has reached the specified distance to the target. If the target is unreachable, the task will fail.
- activity: The move activity the NPC should use while moving (Optional.)
The NPC will play the specified animation. The task will succeed once the animation has finished. If the animation has been interrupted, the task will fail.
The NPC will play the specified activity. The task will succeed once the animation has finished. If the animation has been interrupted, the task will fail.
The NPC will move in a random direction. The task will succeed if the NPC has reached the goal. If the goal is unreachable, the task will fail.
This task will emit a sound from the NPC and immediately return success, regardless of whether the sound could be played or not.
- sound which the task should play. The sound will be emitted from the NPC.