C#/Lua functions executed on wrong instance?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
dreternal
Posts: 5
Joined: Sat Oct 23, 2021 11:13 am

C#/Lua functions executed on wrong instance?

Post by dreternal »

I have two boxes set up as NPCs (actors). Call them Instance01 and Instace02. If I interact with either one, and call a custom FollowMe() function I have in C#, the method is always called on Instance01 even if I'm only interacting with Instance02.

Lua.RegisterFunction("Follow", this, SymbolExtensions.GetMethodInfo(() => Follow()));

It shows up in my Custom function lists ok, and gets executed, just on the wrong GameObject instance.

Ideas?

Windows 11
Unity 2021.1.7f
Dialog System for Unity 2.2.18
User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: C#/Lua functions executed on wrong instance?

Post by Tony Li »

Hi,

Lua function names are registered globally.

If you run Lua.RegisterFunction("Follow"...) on Instance01, then you can't register it again on Instance02. (In the Console window, you'll see a warning message indicating this.)

You could write your FollowMe function to make the current conversation conversant (e.g., NPC) follow the player. Example:

Code: Select all

Lua.RegisterFunction("FollowMe", this, SymbolExtensions.GetMethodInfo(() => FollowMe()));
...
void FollowMe()
{
    YourAIScript npcAI = DialogueManager.currentConversant.GetComponent<YourAIScript>();
    npcAI.FollowPlayer();
}
dreternal
Posts: 5
Joined: Sat Oct 23, 2021 11:13 am

Re: C#/Lua functions executed on wrong instance?

Post by dreternal »

Thanks for that. I have modified my code, but apparently, DialogueManager.currentConversant is always returning the same GameObject, no matter who I'm talking to.
dreternal
Posts: 5
Joined: Sat Oct 23, 2021 11:13 am

Re: C#/Lua functions executed on wrong instance?

Post by dreternal »

Screenshot showing that I am interacting with NPC2 but NPC1 is getting methods called.

User avatar
Tony Li
Posts: 22158
Joined: Thu Jul 18, 2013 1:27 pm

Re: C#/Lua functions executed on wrong instance?

Post by Tony Li »

Hi,

Please see Character GameObject Assignments.

Check what's assigned to the Dialogue System Trigger's Actions > Start Conversation > Conversation Conversant field, and to the NPCs' Dialogue Actor components (if present).

Then temporarily set the Dialogue Manager's Other Settings > Debug Level to Info and reproduce the issue. When a conversation starts, you should see a line in the Console like this:

Dialogue System: Starting conversation 'title' with actor=X and conversant=Y

Check that 'Y' is the GameObject you intend.
Post Reply