Creating a condition: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
No edit summary
(Redirected page to Condition)
 
Line 1: Line 1:
{{npc tut}}
#redirect [[Condition]]
==Declaration==
The first step to creating a condition for your NPC is to create a name for it and add it to the enum. Here's an example name for a condition: '''COND_BLOCKED_BY_DOOR'''.
 
Once the condition is added to the enum, you must use the '''DECLARE_CONDITION''' macro in the '''AI_BEGIN_CUSTOM_NPC''' section.
 
Here's an example of the DECLARE_CONDITION code:
<pre> DECLARE_CONDITION( COND_NEWNPC_CONDITION )</pre>
{{note|It is not necessary to declare shared conditions.}}
 
==GatherConditions==
<code>GatherConditions</code> can be overridden in child classes to implement the selection of custom conditions; the function sets the NPC's conditions appropriate at the time based on game state.
 
The following condition has been taken from the headcrabs <code>GatherConditions()</code> method.
 
<pre>
if( m_lifeState == LIFE_ALIVE && GetWaterLevel() > 1 )
{
          // Start Drowning!
        SetCondition( COND_HEADCRAB_IN_WATER );
}
</pre>
 
This if block checks to see if the headcrab is alive but submerged in water, if so a condition <code>COND_HEADCRAB_IN_WATER</code> is set.
 
==BuildScheduleTestBits==
<code>BuildScheduleTestBits</code> can be overridden to set custom condition to interrupt schedules. If you want to see your NPC react to conditions, this needs to be overriden so the NPC reacts to fire (COND_ON_FIRE) or starts drowing (COND_HEADCRAB_IN_WATER) right away, not after the current schedule ends.
 
Here is code from the headcrabs <code>BuildScheduleTestBits()</code>
 
<pre>
if ( !IsCurSchedule(SCHED_HEADCRAB_DROWN) )
{
// Interrupt any schedule unless already drowning.
SetCustomInterruptCondition( COND_HEADCRAB_IN_WATER );
}
else
{
// Don't stop drowning just because you're in water!
ClearCustomInterruptCondition( COND_HEADCRAB_IN_WATER );
}
</pre>
 
If the condition <code>COND_HEADCRAB_IN_WATER</code> is set, it interrupts the current schedule unless it is already drowning. Then this is handled in <code>SelectSchedule()</code> to choose the appropriate schedule (<code>SCHED_HEADCRAB_DROWN</code>) that will end up drowning the headcrab.
 
==See also==
*[[Conditions]]
*[[Shared conditions]]
 
{{navbar|Giving an NPC Memory|Creating an NPC|Creating a task}}
[[Category:AI Programming]]

Latest revision as of 09:40, 15 September 2011

Redirect to: