Creating a task: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
mNo edit summary
(Redirected page to Task)
 
(7 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{npc tut}}
#redirect [[Task]]
Custom tasks may be added to your NPC by first declaring a new enum.
 
<pre>
enum
{
TASK_JUMP = LAST_SHARED_TASK,
        TASK_FIND_DODGE_DIRECTION,
};
</pre>
 
Then you would need to add the tasks in your custom schedule in the order you wish them to execute, notice <code>TASK_FIND_DODGE_DIRECTION</code> has been given a value of 3, you can pass data through with your tasks and use them when the task logic is executed.
 
<pre>
AI_BEGIN_CUSTOM_NPC( npc_custom, CNPC_Custom )
DEFINE_SCHEDULE
(
SCHED_DODGE_ENEMY_FIRE,
 
" Tasks"
" TASK_FIND_DODGE_DIRECTION 3"
" TASK_JUMP 0"
""
" Interrupts"
        "              COND_LIGHT_DAMAGE"
);
AI_END_CUSTOM_NPC()
</pre>
 
We need to also provide some logic that will be executed for each task, we can do this by overriding the method <code>StartTask( const Task_t *pTask )</code> and <code>RunTask( const Task_t *pTask )</code> of <code>CAI_BaseNPC</code>. As mentioned you can grab the data passed through with the task in these methods by using <code>pTask->flTaskData</code>. An example is shown below.
 
<pre>
void CNPC_Custom :StartTask( const Task_t *pTask )
{
switch ( pTask->iTask )
{
case TASK_FIND_DODGE_DIRECTION:
{
if(!FindBestDodgeDirection(pTask->flTaskData))
                        {
                              TaskFail( "TASK_FIND_DODGE_DIRECTION: Unable to find suitable dodge direction\n" );
                        }
                        else
                        {
                              TaskComplete();
                        }
}
break;
 
case TASK_JUMP:
{
Jump();
}
break;
 
default:
{
BaseClass::StartTask( pTask );
}
}
}
</pre>
{{navbar|Creating a condition|Creating an NPC|Creating an activity}}
[[Category:AI Programming]]

Latest revision as of 03:02, 16 September 2011

Redirect to: