Run OnConversationStart and scripts for Conversant only
Posted: Mon Dec 05, 2022 3:55 pm
EDIT: I am mistaken about the problems; please see follow-up reply!
------------------------------
Hey,
I've got a generic NPC.cs script that I reuse between a lot of NPC prefabs. The NPC.cs script is attached to my "NPC Prefab" GameObject. I want each NPC to be able to detect when the player is talking to them (to activate their Cinemachine camera, turn to face the player, etc). I also have animation triggers set up to play animations for various emotions (Neutral, Happy, Sad) that I want to trigger in specific conversation nodes.
1. I defined some OnConversationStart and OnConversationEnd public methods in NPC.cs, and wired them up via a Dialogue System Events component on the NPC Prefab GameObject.
2. I defined some Lua script handlers using Templates>Scripts>TemplateCustomLua, following your tutorial on YouTube (Dialogue System for Unity 2.x - Connecting Your C# Methods to Lua), and call a script like Neutral() or Happy() in various conversation nodes.
The problem I am running into is that when I talk to one NPC, the OnConversationStart and emotion scripts execute for ALL NPCs, rather than just the Conversant.
I already have a workaround for setting the emotions using a Scene Event (instead of a Script) to point to the specific scene instance of the NPC Prefab GameObject to call the proper method, but that require more work because I have to assign the GameObject for every node that I want to trigger an event.
I already have the Conversation Conversant is set to the parent NPC Prefab GameObject transform in the Dialogue System Trigger. One solution might be to get filter calls in NPC.cs by getting the current conversation's conversant, and check if Conversation Conversant == transform.
Is there a better way to achieve this? Thanks!
-----------
Here's how I wired up OnConversationStart and OnConversationEnd (the view is my NPC prefab hierarchy):
And here's the relevant code from NPC.cs:
------------------------------
Hey,
I've got a generic NPC.cs script that I reuse between a lot of NPC prefabs. The NPC.cs script is attached to my "NPC Prefab" GameObject. I want each NPC to be able to detect when the player is talking to them (to activate their Cinemachine camera, turn to face the player, etc). I also have animation triggers set up to play animations for various emotions (Neutral, Happy, Sad) that I want to trigger in specific conversation nodes.
1. I defined some OnConversationStart and OnConversationEnd public methods in NPC.cs, and wired them up via a Dialogue System Events component on the NPC Prefab GameObject.
2. I defined some Lua script handlers using Templates>Scripts>TemplateCustomLua, following your tutorial on YouTube (Dialogue System for Unity 2.x - Connecting Your C# Methods to Lua), and call a script like Neutral() or Happy() in various conversation nodes.
The problem I am running into is that when I talk to one NPC, the OnConversationStart and emotion scripts execute for ALL NPCs, rather than just the Conversant.
I already have a workaround for setting the emotions using a Scene Event (instead of a Script) to point to the specific scene instance of the NPC Prefab GameObject to call the proper method, but that require more work because I have to assign the GameObject for every node that I want to trigger an event.
I already have the Conversation Conversant is set to the parent NPC Prefab GameObject transform in the Dialogue System Trigger. One solution might be to get filter calls in NPC.cs by getting the current conversation's conversant, and check if Conversation Conversant == transform.
Is there a better way to achieve this? Thanks!
-----------
Here's how I wired up OnConversationStart and OnConversationEnd (the view is my NPC prefab hierarchy):
And here's the relevant code from NPC.cs:
Code: Select all
public void OnConversationStart() {
print("Conversation started");
closeupCamera.SetActive(true);
}
public void OnConversationEnd() {
print("Conversation ended");
closeupCamera.SetActive(false);
}
public void SetEmotion(Emotion emotion) {
print("Setting emotion!");
animator.SetTrigger(emotion.ToString());
}
#region Register with Lua
void OnEnable() {
Lua.RegisterFunction("Neutral", this, SymbolExtensions.GetMethodInfo(() => SetEmotionNeutral()));
Lua.RegisterFunction("Sad", this, SymbolExtensions.GetMethodInfo(() => SetEmotionSad()));
}
void OnDisable() {
Lua.UnregisterFunction("Neutral");
Lua.UnregisterFunction("Sad");
}
public void SetEmotionNeutral() {
SetEmotion(Emotion.Neutral);
}
public void SetEmotionSad() {
SetEmotion(Emotion.Sad);
}
#endregion