Setting an NPC active depending on location.

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Setting an NPC active depending on location.

Post by Strig »

Hello, I'm trying to get a few NPCs to roam across the map in my game. They'll have a variable inside their Actor that says where their current location is. The way I'm checking it is whether or not the NPC's location (which changes based on conversations) is the same as the player's and if so, it activates the NPC.
Captura de Tela (39).png
Captura de Tela (39).png (18.09 KiB) Viewed 685 times
Captura de Tela (40).png
Captura de Tela (40).png (5.77 KiB) Viewed 685 times
The reason it's being done like this is because the game and NPCs are all spread across multiple scenes, so we don't want any reference problems.

I tried a quick C# script to test this, but so far it either seems to always return false, even if I manually set the two Location variables to be the same.
Captura de Tela (42).png
Captura de Tela (42).png (72.47 KiB) Viewed 684 times
Is there a difference in how the variables are called? I thought they were both Locations, so it would be okay to compare them as strings, but apparently something is wrong.

Maybe the variables aren't being immediately set up? Those debug logs I set both return empty.
Captura de Tela (41).png
Captura de Tela (41).png (15.62 KiB) Viewed 685 times
Any ideas on how I could fix this?
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Setting an NPC active depending on location.

Post by Tony Li »

Hi,

Location variables/fields store the location's ID number -- 2 in the case of Basecamp [2].

Check:

Code: Select all

Debug.Log("CurrentLocation: " + DialogueLua.GetVariable("CurrentLocation").asInt);
Are you sure the database with those variables/fields is loaded into memory (e.g., assigned to the Dialogue Manager's Initial Database field)?
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Re: Setting an NPC active depending on location.

Post by Strig »

Hello, thanks for the help! At least now I can check the numbers correctly on Debug.

Interestingly, both the actor's CurrentLocation and the Player's CurrentLocation are returning 0 at start, even though they've been manually set to 2. Maybe it has to do with me invoking the function on Enable? There might be some configuration that briefly resets the variables at start (thus making them read as 0), no?
Captura de Tela (43).png
Captura de Tela (43).png (19.51 KiB) Viewed 681 times
Ah, I can also confirm that the database used on the scene is indeed the correct one.
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Setting an NPC active depending on location.

Post by Tony Li »

Hi,

Try waiting a frame for the database to populate Lua.
Strig
Posts: 40
Joined: Fri Aug 21, 2020 12:33 pm

Re: Setting an NPC active depending on location.

Post by Strig »

So, holding for a frame using a coroutine worked. It now returns the correct value.

However, I do have to ask: this means that the values on the Database are all being returned to 0 after Start and then are set to whatever value I assign to them, right? Is there a way I can change this?
Captura de Tela (44).png
Captura de Tela (44).png (15.52 KiB) Viewed 680 times
P.S.: Additionally, we realized that a similar limitation is set upon the save system. Is it a general thing we have to be aware of when invoking the database variables?
User avatar
Tony Li
Posts: 22049
Joined: Thu Jul 18, 2013 1:27 pm

Re: Setting an NPC active depending on location.

Post by Tony Li »

Here is the order in which things happen:

Awake:
  • Dialogue Manager does basic initialization but not database.
Start:
  • Dialogue Manager loads database content into Lua and registers Lua functions.
  • Dialogue Manager prewarms UI if specified in Other Settings.
  • DialogueManager.instance.initializationComplete event invoked.
You should either wait until the end of the frame:

Code: Select all

IEnumerator Start()
{
    yield return new WaitForEndOfFrame();
    // (Your code here that accesses Lua variables.)
}
Or hook into DialogueManager.instance.initializationComplete:

Code: Select all

void Awake()
{
    DialogueManager.instance.initializeComplete += OnInitialized;
}

void OnInitialized()
{
    DialogueManager.instance.initializeComplete += OnInitialized;
    // (Your code here that accesses Lua variables.)
}

In version 2.2.16, I've added a refactor that loads the Initial Database content into Lua by the end of Awake, so it will be available in OnEnable and Start. It isn't that way in 2.2.15 because of some preloading code. The Dialogue Manager's Other Settings > Preload Resources will still work, however.
Post Reply