NPC Action Point

From RPG Creation Kit
Jump to navigation Jump to search
An NPC Action Point placed in the first dungeon that allows the NPC to rest on a chair.

NPC Action Points are special Objects placed in the world that NPCs can use to perform actions, such as sitting on a chair, leaning on a wall or a counter, working on a farm or sleeping in a bed.

They are what makes NPCs feel alive while they are not doing anything else, instead of standing still or wandering around, an NPC can reach an Action Point, play an animation on it and stay there until its Behaviour tells it to do something else.

You can find some ready-to-use Action Points in the Prefab Library, such as NPCActionPoint_Farm001, NPCActionPoint_LeanOnCounter, NPCActionPoint_LeanOnWallFromFront and Chair04_ActionPoint, and you can obviously create your own with your custom animations.

How Action Points work

An Action Point is a GameObject with the "NPC Action Point" component attached to it. When an NPC is told to use an Action Point (usually from its Behaviour Tree), the following happens:

  1. The NPC walks to the Pivot Point of the Action Point using the NavMesh.
  2. Once it is close enough, the NPC gets fixed in place, its position and rotation are adjusted to match exactly the Pivot Point.
  3. The NPC plays the Npc Action animation (for example, the sitting down animation).
  4. In the AI's Animator, the Npc Action will finish playing and go straight to a single (or multiple) idle loop animations.
  5. When the NPC has to leave the point, it plays the Npc Action Return animation (for example, getting up from the chair) and returns to its normal behaviour.

While an NPC is using or reaching an Action Point, the point is flagged as occupied, so no other NPC will pick it. If the NPC enters in combat while reaching or using the point, he will stop using the Action Point and go into the combat behavior.

The NPC Action Point component

Actionp 2.png

  • Action Point ID: the UNIQUE ID of this Action Point, it is used to register the point in the Cell and to reference it from Behaviour Trees. You can generate a new one by right-clicking on the component and selecting "Regenerate ID".
  • Action Type: the type of action this point represents (Rest, Eat, Sleep, Mount). It acts as a tag, so it is possible to search for points of a given type (for example, an NPC that wants to find a place to Eat).
  • Show Dummies: if enabled, the Dummies will be shown in the Scene View while the Action Point is selected (see the section below).
  • Dummy First Keyframe / Dummy Last Keyframe: references to the two Dummies used to preview the animation in the Editor.
  • Is Occupied: runtime flag that states whether an NPC is currently using (or reaching) this point.
  • Pivot Point: the transform the NPC will be placed on before playing the animation. The placement of the Pivot Point is what makes the animation look right, if your NPC sits in the air or inside the chair, this is what you have to adjust.
  • Npc Action: the animation the NPC plays when it starts using the point (sitting down, leaning on the wall...).
  • Npc Action Return: the animation the NPC plays when it stops using the point (getting up, detaching from the wall...).
  • Entity Using This Point: runtime reference to the Entity that is currently using this point.
  • Spawn Dynamic Object: if enabled, a Dynamic Object will be spawned in one of the NPC's hands while it is performing the action (see the Dynamic Objects section).

As you may have noticed, the "loop" animation that plays after the Npc Action is nowhere to be seen here. That (or those) are set in the TPSBase Animator itself:

Npcaction 3.png

Placing an Action Point with the Dummies

Placing an Action Point by trial and error would be pretty tedious, you would have to enter Play Mode every time just to check if the NPC sits correctly. This is what the Dummies are for.

An Action Point has two Dummies as children: one is sampled at the first keyframe of the Npc Action animation, the other one at the last keyframe. With Show Dummies enabled, selecting the Action Point in the Scene View will show you exactly where the NPC will stand when it starts the action and where it will end up once the animation is over.

Actionp 1.png

So, to place an Action Point on a chair for example, all you have to do is drag one of the prefabs near the chair, select it, and move it until the last keyframe Dummy sits correctly on the chair. The Dummies are automatically disabled when the game starts, so you don't have to worry about them being visible in game.

Registering the Action Point in the Cell

Action Points live inside Cells, and every Cell holds a dictionary of all the Action Points it contains (you can see it in the "Cell Information" component, in the All Action Points dictionary). NPCs will search this dictionary when they are looking for a point to use.

You do not have to fill this dictionary by hand, after placing your Action Points in the Cell, simply run Update the Cell and they will be registered automatically by their Action Point ID.

Note: Make sure every point has a unique ID, you can use the "Regenerate ID" context menu for that.

Making an NPC use an Action Point

NPCs use Action Points through their Behaviour Trees.

You'll probably only want to switch their Behavior Tree to one of these Behaviors:

  • SBTP_UseCurrentActionPoint: The NPC will go and use the Action Point that he has assigned in his RckAI component.
  • SBTP_UseCurrentActionPointNoLookAt: Same thing as above, but the LookAt will be disabled for the AI using the action point (used for example on the blacksmith, to avoid him move his body if someone gets closer and mess the animation).
  • Something like SBPT_FarmersLeftVirrhiael01: where you can assign an array of actions points and the NPCs will randomly pick some.


Behaviors will be calling one of the following methods with an Invoke Method node:

  • GetAndUseActionPoint: the NPC asks its current Cell for a random unoccupied Action Point and uses it. If every point of the Cell is occupied, the flag failedGettingActionPoint is set to true, so you can handle this case in the Behaviour Tree.
  • UseCurrentActionPoint: the NPC uses the Action Point that is currently assigned in the Action Point field of its AI Movements. This is useful when you want a specific NPC to always use a specific point, like the farmers of the Demo.
  • UseArrayActionPoint: takes an array of Action Point IDs as parameter, the NPC will pick a random unoccupied point among them. Useful when an NPC should choose between a set of points you've defined (for example, only the chairs of the tavern and not the leaning on the counter action).
  • StopUsingNPCActionPoint: the NPC leaves the point, playing the Npc Action Return animation. If the immediate parameter is set to true, the NPC will skip the return animation and go back to its locomotion instantly.
  • StopUsingNPCActionPointImmediate: same as above with immediate always true, in case you cannot pass a parameter.

You can also check flags such as isReachingActionPoint, isUsingActionPoint, leavingActionPoint and failedGettingActionPoint in your Behaviour Trees conditions to know at which state of the process the NPC is.

Dynamic Objects

Some actions look much better with a prop in the NPC's hands, think of the farmers of the Demo working the fields with their farming tool or the blacksmith working on his weapons with his hammer. That tool does not exist in the NPC's inventory, it is a Dynamic Object spawned by the Action Point itself.

Crafting.png

To set it up, enable Spawn Dynamic Object and set:

  • Dynamic Object: the prefab to spawn (the tool, like DoSmithingHammerPrefab). Its Prefab position should be set such that it will be correctly placed to the choosen Dynamic Object Bone.
  • Dynamic Object Bone: the hand where the object will be attached (Rhand or Lhand).
  • Dynamic Object Sound: an optional AudioClip to play during the action (requires the Dynamic Object to have an AudioSource).
  • Dynamic Object Particles: optional particles to play during the action.

The spawning is driven by Animation Events placed on the Npc Action animation clip, so that the object appears and plays sounds and particles in sync with the animation. The events you can call are: DynamicObjectStartAnimationEvent, DynamicObjectEndAnimationEvent, DynamicObjectPlaySoundAnimationEvent and DynamicObjectPlayParticlesEvent. You can see how they are placed by looking at the animation used by the Blacksmith:

  • DoSmithingEnter: will have DynamicObjectStartAnimationEvent that will allow us to spawn the Dynamic Object.
  • DoSmithingLoop: will have DynamicObjectPlaySoundAnimationEvent to play a sound at a certain frame, and DynamicObjectPlayParticlesEvent to fire the particles at a certain frame.
  • DoSmithingRelease: will have DynamicObjectEndAnimationEvent to destroy the Dynamic Object.

Scripting custom Action Points

The NPC Action Point component exposes a few virtual methods you can override to create your own kind of Action Point for more complex scenarios:

  • OnUserAssigned: called as soon as an NPC picks this point, so when it starts reaching it (it does not mean it is using it yet).
  • OnUserUses: called when the NPC starts playing the enter animation on the point.
  • OnUserIsLeaving: called when the NPC starts playing the return animation.
  • OnUserHasLeft: called when the NPC has completely been released from the point.
  • OnUserForceLeaves: called when the NPC gets reset and should no longer reach or use the point (for example, when it enters in combat).

A good example of this inside the kit is the Mountup Point, which is a special Action Point placed on Mounts that NPCs use to get on the saddle. Note that Mountup Points are excluded from the Cell's Action Points dictionary, they are handled by the Mounts system.