Creating a task: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (navbar)
mNo edit summary
Line 62: Line 62:
}
}
</pre>
</pre>
{{navbar|Defining the NPC|Creating an NPC|Creating an activity}}
{{navbar|Creating a condition|Creating an NPC|Creating an activity}}
[[Category:AI Programming]]
[[Category:AI Programming]]

Revision as of 23:56, 17 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 );
		}
	}
}