Creating a New Persistent Reference AI

From RPG Creation Kit
Jump to navigation Jump to search

In this tutorial we are going to see how you can add a new Persistent Reference AI.

To know more about Persistent References, refer to this article.

We will make a guard that will follow the WandererMerchant001 on his path.

Creating the New NPC

To create the new guard, I'm going to duplicate the NPC "CityGuard001" and do the following changes:

  • Change its ID to WandererGuard001.
  • Change its Name to Wanderer Guard.
  • Tick the Persistent Reference box.

Newprefai 1.png

Once you do that and click "Save", the AI will automatically be registered as a Persistent Reference and placed in the _PersistentReferences_ scene.

I'll go ahead and place the WandererGuard001 next to the WandererMerchant001 in the _PersistentReferences_ scene.

Setting up the NPC

Persistent Reference AI have two special fields that are visible from the RckAI Inspector, in the Entity tab in particular:

Newprefai 2.png

These represent the current location of the Persistent Reference.

There are two important things to remember:

  • If the WorldSpaceID set is an Exterior Worldspace, there is no need to set the "In Cell ID" field, the AI will be fully loaded if the player gets close to this AI.
  • If the WorldSpaceID set is an Interior WorldSpace, you need to provide the "In Cell ID" field.

Since our guard travels the Virrihael Exterior Worldspace, we only need to set the "InWorldSpaceID" to "VirrihaelWorldspace".

It is important to notice that at anytime you can change these fields to teleport the AI to new WorldSpaces and cells, which makes the use of Persistent Reference AI pretty convenient.

With that, you have your Persistent Reference set, and the next steps are only to set the behavior of the NPC.

Setting the NPC's Behavior

You could simply assign the NPC to follow the same path as the WandererMerchant001, but there may be few issues with it.

If the player talks to the WandererMerchant NPC, he will stop him while the guard would keep walking the path. Or if you talk to the guard, when you release him he will always walk at the same speed at the WandererMerchant, so they will be forever be out of synch.

We can assign to the WandererGuard001 the behavior "[BTree] [P] Follower Dynamic (MainTarget)", which will make the guard follow its main target and if the distance is significant, run to it. So it's perfect for our use case!

The only thing we need to do is to assign as the Guard's MainTarget the WandererMerchant, so he knows who to follow.

In the RckAI Inspector of the WandererGuard001, set the MainTarget to be the WandererMerchant001:

Newprefai 4.png

Since we want the Guard to always follow the Main Target, even if in OfflineMode, also tick the box "Always cache target pos?":

Newpref 6.png

Assigning the Purpose

Everything works great, but there is one major problem.

If the guard will encounter an enemy, after he kills him he will not resume its purpose, therefore he will not follow the Wanderer Merchant no more.

This is because we haven't assigned any purpose.

Before going on, make sure to read this article, especially the paragraph "Purpose State, the Memory of the AI".

Assigning the purpose is very easy, you can do it in the Behaviour Tab in the RckAI Inspector.

Newprefai 5.png

Let's look at what I did:

  • AI: Is the entity you want to assign the purpose to, it will always be the same RckAI.
  • IsAssigned: is a flag that states whether the purpose is assigned or not.
  • PTarget: represents the MainTarget of the Purpose Behavior, in our case, it's the WandererMerchant001.
  • ITargetableID: is the ID of the PTarget.
  • ITargetableType: is the type of the PTarget, here is the full list:
    • Entity = 0,
    • Door = 1,
    • PRef = 2,
    • Transform = 3,
    • AIPath = 4
  • ITargetableExtraData: is an extra data that may be used in your scripts if needed.
  • ClearsOn: represents the event that needs to occur for completing this Purpose
  • NextPurposeBehavior: is the ID of the Behaviour you want to set your AI to when this purpose is completed.

Once you did that, you're all set. The guard will act accordingly.


You may be wondering why we had to manually assign the purpose of the WandererGuard001, while the purpose of the WanderingMerchant001 is not assigned. This is for a very simple reason, because the Behaviour that the Merchant use automatically assigns a purpose. You can see it in the Behaviour Tree, there's a specific AI_Invoke call that performs the assignment of the purpose.

The Purpose State is very flexible, and it is vastly used throughout the demo, sometimes it's more convenient to manually assign/clear purposes with Quest Stages Scripts and QuestScripts during the development of a quest.