How to read return value from action back into NPC chat

Hello there,

I have done my best to find the answer, but my search hasn’t returned anything and I’ve been at it a while…

I am using NPC AI Engine for Unity version 3.3.0.

I have an AI character who has a narrative design setup with a few stages. These stages instruct the NPC to trigger some custom actions that I have created. This is all working and I can interact with the character in my scene as expected - all good so far…

However, I have one action that looks inside the player’s inventory and does a calculation, which stores an integer for later use and also (try to) return the value back as a response. Basically for context, I am creating a tax collector character, whom the player has to bargain with to reduce a fee they need to pay.

The problem is that the action’s return value is not visible to the NPC. I want the NPC to say something like “You owe [number] gold”. The NPC does indeed fill in a number, but it’s made up. It is not returning the value from the action…

For simplicity sake: Here’s a simple version of the code - calculatedTax is a global variable int:

public int CalculateTax()

{

    calculatedTax = 50;

    return calculatedTax;

}

I have this in DoAction - as mentioned, the action is working, because I use it the calculated value later in another action and I can see debug messages.

case ActionChoice.CalculateTax:
     yield return CalculateTax();
     break;

I have also tried returning action result to the Convai log - something like this:

if (_currentNPC != null)

{

    ConvaiLogger.Info($"ACTION_RESULT: {calculatedTax}",      ConvaiLogger.LogCategory.Actions);

}

I can see the action result in the log as well, but I don’t know how it is used by the NPC. Certainly it wasn’t working for me.

My narrative design basically just consists of a section that says:

Call the “CalculateTax” action.

use the value returned from the CalculateTax action to tell the user how much they owe…

Basically at the appropriate time, I would see the value 50 being calculated, so the action is being called, but my NPC would say “you owe 10 gold”, then at the given time, it would collect 50 gold from the player.

What am I doing wrong please. What else do you need to know to further diagnose?

I have since found the notion of adding variables to narrative design using the curly brackets. I have done this. I have also added Narrative Design Key Controller to my NPC and can specify a value, which is correctly pulling through. However, this still doesn’t enable me to generate a dynamic value and return it. Please help.

I have now discovered I can dynamically update from my code the narrative design keys. I’ve got this function that I am now calling from CalculateTax:

private void UpdateNarrativeVariable(string key, string value)
{
    if (_currentNPC != null)
    {
      var keyController = _currentNPC.GetComponent<NarrativeDesignKeyController>();

      if (keyController != null)
      {
          var narrativeKey = keyController.narrativeDesignKeys.Find(k => k.name == key);

          if (narrativeKey != null)
          {
              narrativeKey.value = value;
              Debug.Log($"Updated narrative variable: {key} = {value}");
          }
          else
etc etc

I can see from the logs it’s correctly setting the value… However, the NPC uses the “old” value, as in the hard coded on in the narrative design key. As if the update is happening too late.

Anyone able to help with this last hurdle? I feel I’m almost there…