Creating a task: Difference between revisions
Jump to navigation
Jump to search
No edit summary |
No edit summary |
||
| Line 1: | Line 1: | ||
{{npc tut}} | {{npc tut}} | ||
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> | |||
{{ | <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> | |||
Revision as of 17:29, 10 June 2006
Custom tasks may be added to your NPC by first declaring a new enum.
enum
{
TASK_JUMP = LAST_SHARED_TASK,
TASK_FIND_DODGE_DIRECTION,
};
Then you would need to add the tasks in your custom schedule in the order you wish them to execute, notice TASK_FIND_DODGE_DIRECTION has been given a value of 3, you can pass data through with your tasks and use them when the task logic is executed.
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()
We need to also provide some logic that will be executed for each task, we can do this by overriding the method StartTask( const Task_t *pTask ) and RunTask( const Task_t *pTask ) of CAI_BaseNPC. As mentioned you can grab the data passed through with the task in these methods by using pTask->flTaskData. An example is shown below.
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 );
}
}
}