Behaviors: Difference between revisions

From Valve Developer Community
Jump to navigation Jump to search
m (cat)
No edit summary
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 assign schedules and functionality to more than one NPC class without having to re-write or derive off a certain class.


----
For example:


For example: <br>
You have three NPC classes : <code>npc_bob</code> , <code>npc_fred</code> , <code>npc_jane</code>
You have three npc classes : npc_bob , npc_fred , npc_jane<br>
You make two behaviors : MensBathroomBehavior, WomensBathroomBehavior<br>


You'd assign MensBathroomBehavior to the male npcs and WomensBathroomBehavior to npc_jane.
You make two behaviors : <code>MensBathroomBehavior</code> and <code>WomensBathroomBehavior</code>


You'd assign <code>MensBathroomBehavior</code> to the male NPCs and <code>WomensBathroomBehavior</code> to <code>npc_jane</code>.


----
==Steps to add a Behavior:==


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


*add a function to your npc class : bool npc_class::CreateBehaviors() : (comes in virtually from CAI_BehaviorHost)
To allow the Behavior to be used you'll need a function in the behavior:
*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 teh behavior:
bool CAI_WhateverBehaviour::CanSelectSchedule()
{
return true;
}


bool CAI_WhateverBehaviour::CanSelectSchedule()
There are a few more things that are different between an npc class and a behavior class : <br>
{
return true;
}


*There's 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)
AI_BEGIN_CUSTOM_NPC -> AI_BEGIN_CUSTOM_SCHEDULE_PROVIDER (body)<br>
DEFINE_CUSTOM_AI -> DEFINE_CUSTOM_SCHEDULE_PROVIDER (header)
DEFINE_CUSTOM_AI -> DEFINE_CUSTOM_SCHEDULE_PROVIDER (header)<br>


That should do it.  Take a look at something like AssaultBehavior if you have any trouble.
That should do it.  Take a look at something like AssaultBehavior if you have any trouble.


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

Revision as of 09:50, 24 October 2006

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;
}

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.