Behaviors

From Valve Developer Community
Jump to navigation Jump to search

Behaviors let you assign schedules and functionality to more than one NPC class without having to re-write or derive off a certain class.

For example:

You have three NPC classes : npc_bob , npc_fred , npc_jane

You make two behaviors : MensBathroomBehavior and WomensBathroomBehavior

You'd assign MensBathroomBehavior to the male NPCs and WomensBathroomBehavior to npc_jane.

Steps to add a Behavior:

  • Add a function to your NPC class : bool npc_class::CreateBehaviors() (comes in virtually from CAI_BehaviorHost)
  • create the behaviour as a member variable in your class
  • Add the behavior AddBehavior( &m_WhateverBehavior ); in your CreateBehaviors() function
  • Let your npc class know you want to use a behavior by calling BehaviorSelectSchedule() in your SelectSchedule().

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

bool CAI_WhateverBehaviour::CanSelectSchedule()
{
	return true;
}

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.

There are a few more things that are different between an npc class and a behavior class :

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.