Behaviors: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
Behaviors let you assign schedules and functionality to more than one NPC class without having to re-write or derive off a certain class.
'''Behaviors''' let you abstract AI code to a C++ class of its own which is not tied to any one NPC. You might want to create <code>MensBathroomBehavior</code> for male NPCs and <code>WomensBathroomBehavior</code> for female NPCs, for instance.


For example:
== Authoring a behavior ==


You have three NPC classes : <code>npc_bob</code> , <code>npc_fred</code> , <code>npc_jane</code>
Behavior code, for the most part, behaves in the same way and uses the same framework as AI code you would write directly into an NPC. There is a certain amount of 'glue' required between one and the NPCs that will use it however:


You make two behaviors : <code>MensBathroomBehavior</code> and <code>WomensBathroomBehavior</code>
=== The behavior ===


You'd assign <code>MensBathroomBehavior</code> to the male NPCs and <code>WomensBathroomBehavior</code> to <code>npc_jane</code>.
*Create <code>[[bool]] CanSelectSchedule()</code>, which defines whether or not an NPC will 'defer' to the behavior for a given [[NPCThink()|think]].
*Create <code>virtual const char *GetName() { return "MyBehavior friendly name"; }</code>.
*Use <code>AI_BEGIN_CUSTOM_SCHEDULE_PROVIDER</code> instead of <code>[[AI_BEGIN_CUSTOM_NPC]]</code> (in the body).
*Use <code>DEFINE_CUSTOM_SCHEDULE_PROVIDER</code> instead of <code>[[DEFINE_CUSTOM_AI]]</code> (in the header).
*Use <code>[[GetOuter()]]-></code> to access the NPC object.


==Steps to add a Behavior:==
=== The NPC ===


*Add a function to your NPC class : <code>bool npc_class::CreateBehaviors()</code> (comes in virtually from <code>CAI_BehaviorHost</code>)
*Include the header for your new behavior, and <code>ai_behavior.h</code>.
*create the behaviour as a member variable in your class
*Create a member instance of your behavior.
*Add the behavior <code>AddBehavior( &m_WhateverBehavior );</code> in your <code>CreateBehaviors()</code> function
*Register the instance in <code>CreateBehaviors()</code> with <code>AddBehavior( &m_MyBehavior )</code>.
*Let your npc class know you want to use a behavior by calling <code>BehaviorSelectSchedule()</code> in your <code>SelectSchedule()</code>.
**None of Valve's NPC base classes have any behaviors registered - it may not be possible to do it at that level.
*Call <code>BehaviorSelectSchedule()</code> at some point in <code>[[SelectSchedule()]]</code>. This probably already happens if you're working with a stock NPC.


To allow the Behavior to be used you'll need a function in the behavior:
== Activating a behaviour manually ==


  bool CAI_WhateverBehaviour::CanSelectSchedule()
Behaviours are evaluated on every [[NPCThink()|think]] with the logic in <code>CanSelectSchedule()</code>. If you want to manually enable a behavior until you disable it:
 
  MyNPC::SelectSchedule()
  {
  {
  return true;
  if ( m_MyBehavior.m_bActive )
{
DeferSchedulingToBehavior( &m_MyBehavior ); ''// Normally happens in BehaviorSelectSchedule()''
return BaseClass::SelectSchedule(); ''// This will now poll your behavior''
}
...
  }
  }


If your behaviour is temporary, the best way is for the designers to use an Input and activate the behaviour when they need it.  In the input specify an active variable toggle and replace the above by returning the active variable.
{{warning|Until you release it, this will lock the NPC into your behavior code and ''only'' your behavior code.}}
 
There are a few more things that are different between an npc class and a behavior class : <br>
 
AI_BEGIN_CUSTOM_NPC -> AI_BEGIN_CUSTOM_SCHEDULE_PROVIDER (body)
DEFINE_CUSTOM_AI -> DEFINE_CUSTOM_SCHEDULE_PROVIDER (header)
 
That should do it.  Take a look at something like AssaultBehavior if you have any trouble.


[[Category:AI]]
[[Category:AI Programming]]

Latest revision as of 10:19, 28 May 2008

Behaviors let you abstract AI code to a C++ class of its own which is not tied to any one NPC. You might want to create MensBathroomBehavior for male NPCs and WomensBathroomBehavior for female NPCs, for instance.

Authoring a behavior

Behavior code, for the most part, behaves in the same way and uses the same framework as AI code you would write directly into an NPC. There is a certain amount of 'glue' required between one and the NPCs that will use it however:

The behavior

  • Create bool CanSelectSchedule(), which defines whether or not an NPC will 'defer' to the behavior for a given think.
  • Create virtual const char *GetName() { return "MyBehavior friendly name"; }.
  • Use AI_BEGIN_CUSTOM_SCHEDULE_PROVIDER instead of AI_BEGIN_CUSTOM_NPC (in the body).
  • Use DEFINE_CUSTOM_SCHEDULE_PROVIDER instead of DEFINE_CUSTOM_AI (in the header).
  • Use GetOuter()-> to access the NPC object.

The NPC

  • Include the header for your new behavior, and ai_behavior.h.
  • Create a member instance of your behavior.
  • Register the instance in CreateBehaviors() with AddBehavior( &m_MyBehavior ).
    • None of Valve's NPC base classes have any behaviors registered - it may not be possible to do it at that level.
  • Call BehaviorSelectSchedule() at some point in SelectSchedule(). This probably already happens if you're working with a stock NPC.

Activating a behaviour manually

Behaviours are evaluated on every think with the logic in CanSelectSchedule(). If you want to manually enable a behavior until you disable it:

MyNPC::SelectSchedule()
{
	if ( m_MyBehavior.m_bActive )
	{
		DeferSchedulingToBehavior( &m_MyBehavior );	// Normally happens in BehaviorSelectSchedule()
		return BaseClass::SelectSchedule();		// This will now poll your behavior
	}

	...
}
Warning.pngWarning:Until you release it, this will lock the NPC into your behavior code and only your behavior code.