Best practices for moving NPCs between scenes

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
moetan14
Posts: 11
Joined: Mon Nov 25, 2019 11:18 pm

Best practices for moving NPCs between scenes

Post by moetan14 »

Hey it's been a while.

I'm working on a quest right now for my game and stumbled upon the following problem.

My maps are pretty segmented with loading screens in between, so if the player talks to an NPC, and said NPC tells the Player to meet somewhere else in another scene, what would be an optimal way to do so with Dialogue System? I have some ideas but it's been the case a lot of the time where I come up with a convoluted solution only to find this framework already has a good solution built in.

What I want to do is the following:

>Talk to NPC in Scene 1, they tell players to meet up somewhere else in Scene 2
>After finishing the conversation the screen fades to black, the NPC disappears, then fade back to normal
>Players go to Scene 2 and meet with the NPC who is now there and has another conversation loaded (or continues an old one, who knows? maybe that's the right way to go about it).

My solution right now would be to have the same NPC in Scene 2 but disabled, then write a script that enables it once the condition has been met, but frankly I have no clue if this is already a common scenario that Dialogue System can handle.

Bear in mind that I also do not know how to fade out, make NPC disappear then fade in. I tried to do it but it happens during the dialogue node, so that should tell you all about how bright I am, lol.

Hopefully it's clear enough to get some help. Thanks in advance!
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practices for moving NPCs between scenes

Post by Tony Li »

Hi,
moetan14 wrote: Wed Sep 22, 2021 11:13 pmMy solution right now would be to have the same NPC in Scene 2 but disabled, then write a script that enables it once the condition has been met, but frankly I have no clue if this is already a common scenario that Dialogue System can handle.
That's a common way to do it. Make a prefab of your NPC. Put an instance of the prefab in scene 1 and another instance in scene 2. Deactivate the one in scene 2. On an active GameObject (empty GameObject is fine), add a Dialogue System Trigger with these settings:
  • Trigger dropdown set to OnSaveDataApplied. (It will run when the scene transition is done or when the scene is started.)
  • Conditions set to check if the quest is in a specific state (e.g., "Meet me in scene 2" state is active).
  • Actions > Set GameObject Active or Actions > OnExecute() Event activates the NPC GameObject.
moetan14 wrote: Wed Sep 22, 2021 11:13 pmBear in mind that I also do not know how to fade out, make NPC disappear then fade in. I tried to do it but it happens during the dialogue node, so that should tell you all about how bright I am, lol.
If you want to do it during the dialogue node, use a sequence like this:

Code: Select all

Fade(stay,1); // Fade to black over 1 second.
SetActive(YourNPCName, false)@1; // Deactivate NPC at the 1-second mark.
Fade(in,1)@1 // Fade in over 1 second at the 1-second mark.
If you don't want it to happen during a conversation, you can play the same sequence above manually in C# using DialogueManager.PlaySequence("sequence") or in a Dialogue System Trigger's Actions > Play Sequence field.
User avatar
moetan14
Posts: 11
Joined: Mon Nov 25, 2019 11:18 pm

Re: Best practices for moving NPCs between scenes

Post by moetan14 »

Thanks for the reply. This is way better than the convoluted thing I was about to do on my own.

One thing though. The set active line in the sequence doesn't seem to be working for me. I wrote the name of the NPC in there and it won't go away, only if I use scene events or the enable/disable action, but since it's separate from the sequence I can't use it in tandem with the fade in/out.
Unity_ptNAPAmjnL.png
Unity_ptNAPAmjnL.png (6.42 KiB) Viewed 1093 times
Unity_BwSfjd0tFz.png
Unity_BwSfjd0tFz.png (1.53 KiB) Viewed 1093 times
The fade works as intended, it's just the setactive line that's giving me issues.
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practices for moving NPCs between scenes

Post by Tony Li »

Hi,

Are there any warnings in the Console window, such as the one below?

Dialogue System: Sequencer: SetActive() command: subject 'gyaru-chan' is null

(Make sure the Console window isn't filtering out warnings, and that your Dialogue Manager's Other Settings > Debug Level is set to Warnings or Info.)

The GameObject name has to match exactly, including upper/lowercase.
User avatar
moetan14
Posts: 11
Joined: Mon Nov 25, 2019 11:18 pm

Re: Best practices for moving NPCs between scenes

Post by moetan14 »

Tony Li wrote: Thu Sep 23, 2021 8:13 pm Hi,

Are there any warnings in the Console window, such as the one below?

Dialogue System: Sequencer: SetActive() command: subject 'gyaru-chan' is null

(Make sure the Console window isn't filtering out warnings, and that your Dialogue Manager's Other Settings > Debug Level is set to Warnings or Info.)

The GameObject name has to match exactly, including upper/lowercase.
The sequence works, SetActive included, if the trigger is on another object and not the NPC itself (documentation says the latter won't work, since it's part of the conversation. It also won't work during a conversation for this reason, I think). So for testing purposes I made it so this object triggers the sequence when the conversation with gyaru-chan is over (with a trigger inside the NPC set to on conversation end to enable it), and it worked, but the NPC goes back to active when I return to the scene. The NPC has all the save system stuff in place, so it's a bit of a mystery as to why it won't stay disabled.

Unity_VyEThXs4K8.png
Unity_VyEThXs4K8.png (22.92 KiB) Viewed 1091 times
On the matter of the console errors, I do get this whenever I start a conversation with every NPC so far in the game.
Unity_IEuN6F3PAz.png
Unity_IEuN6F3PAz.png (15.52 KiB) Viewed 1091 times
But I doubt this has anything to do with what I'm trying to do. It's been there this whole time but never cause problems.

So yeah I don't exactly know what I'm doing here. I'm clearly missing something in the process.

Edit: I just realized what I wrote is a bit confusing. The NPC in question is always gyaru-chan, it's not an NPC out of the conversation.
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practices for moving NPCs between scenes

Post by Tony Li »

Hi,
moetan14 wrote: Thu Sep 23, 2021 8:35 pmThe sequence works, SetActive included, if the trigger is on another object and not the NPC itself (documentation says the latter won't work, since it's part of the conversation. It also won't work during a conversation for this reason, I think). So for testing purposes I made it so this object triggers the sequence when the conversation with gyaru-chan is over (with a trigger inside the NPC set to on conversation end to enable it), and it worked, but the NPC goes back to active when I return to the scene. The NPC has all the save system stuff in place, so it's a bit of a mystery as to why it won't stay disabled.
Add an ActiveSaver to the NPC in scene 1, and assign the NPC GameObject itself to the GameObject To Watch field. This component will save the NPC GameObject's active/inactive state.

Note: If the NPC's GameObject starts inactive at design time (i.e., before entering play mode), you must put the ActiveSaver on a different, active GameObject instead, and assign the NPC to the GameObject To Watch field.

You can use a scene event if you have difficulty getting SetActive() to work.
moetan14 wrote: Thu Sep 23, 2021 8:35 pmOn the matter of the console errors, I do get this whenever I start a conversation with every NPC so far in the game.
<ArgumentException>
A UnityEvent is misconfigured. Check the Dialogue Manager and Player for components that have UnityEvents, such as DialogueSystemEvents components.
User avatar
moetan14
Posts: 11
Joined: Mon Nov 25, 2019 11:18 pm

Re: Best practices for moving NPCs between scenes

Post by moetan14 »

Alright! the active saver worked like a charm, but it had to go on an empty object and not the NPC for it to work. Thanks a lot for the guindance!
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Best practices for moving NPCs between scenes

Post by Tony Li »

Ah, good catch. I forgot that if the ActiveSaver was on the NPC and the NPC was inactive, the ActiveSaver wouldn't be active to save its state when changing scenes.
Post Reply