Creating a task: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
No edit summary
Line 1: Line 1:
{{npc tut}}
{{npc tut}}
==Declaration==
Custom tasks may be added to your NPC by first declaring a new enum.
The first step to creating a task for your NPC is to create a name for it and add it to the enum. Here's an example name for a task: '''TASK_COMBINE_DIE_INSTANTLY'''.


Once the task is added to the enum, you must use the '''DECLARE_TASK''' macro in the '''AI_BEGIN_CUSTOM_NPC''' section.
<pre>
enum  
{
TASK_JUMP = LAST_SHARED_TASK,
        TASK_FIND_DODGE_DIRECTION,
};
</pre>


Here's an example of the DECLARE_TASK code:
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> DECLARE_TASK( TASK_NEWNPC_TASK )</pre>
 
{{note|It is not necessary to declare tasks inherited from the BaseClass}}
<pre>
==Implementation==
AI_BEGIN_CUSTOM_NPC( npc_custom, CNPC_Custom )
{{todo|Implementation}}
DEFINE_SCHEDULE
{{navbar|Defining the NPC|Creating an NPC|Creating an activity}}
(
[[Category:AI Programming]]
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 18: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 );
		}
	}
}