Prefab Dialogue trigger System using script variables

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
User avatar
rykaan
Posts: 75
Joined: Tue Feb 11, 2020 6:22 am

Prefab Dialogue trigger System using script variables

Post by rykaan »

Greetings!

I've recently picked up the dialogue system for unity and it looks like its massively going to help my project so thank you for creating the tool. I am however having an issue I can't work out.

I'm working through creating a Prefab for all the enemies in my game; setting it up with every generic script/collider/etc I can before customising them later. One of these prefabs is a Dialogue System Trigger to 'catch' the player and start a conversation so they can be thrown out of the area.

I can set up the trigger to start the conversation when they are close. But I need to also check a bool to confirm if the enemy can see the player. A dialogue database variable (bool) would become true if any enemy could see the player, so I could end up with a false positive. What would the best method be to have the Dialogue System Trigger check a bool from a script attached to the same GameObject (or parent of)?.

Thanks in Advance,
Rob
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prefab Dialogue trigger System using script variables

Post by Tony Li »

I recommend writing a C# method that returns the value of the bool, something like CanSeePlayer("NPC"). Given an NPC name, it returns the bool on that NPC's script. If each NPC has a Dialogue Actor component, you can quickly look up the NPC by calling CharacterInfo.GetRegisteredActorTransform("NPC Name"). Then register it with Lua and call it in the Conditions > Lua Conditions. Example:

Code: Select all

public static bool CanSeePlayer(string npcName)
{
    var myScript = CharacterInfo.GetRegisteredActorTransform(npcName).GetComponent<MyScript>();
    return myScript.myBool; // (Ideally you'll add some null-checking, which I omitted for clarity.)
}

void Awake()
{ // Register function with Lua:
    Lua.RegisterFunction("CanSeePlayer", null, SymbolExtensions.GetMethodInfo(() => CanSeePlayer(string.Empty)));
}
luaFuncCondition.png
luaFuncCondition.png (36.84 KiB) Viewed 716 times
User avatar
rykaan
Posts: 75
Joined: Tue Feb 11, 2020 6:22 am

Re: Prefab Dialogue trigger System using script variables

Post by rykaan »

Thank you so much for your help.

I had been looking through the manual and was trying to get the LUA functions scripting to work, but was struggling. The example you gave was exactly what I needed. It's set up now so I should just be able to copy paste the enemy and change the Actor name + the corresponding LUA condition search name.

As a quick follow up question. If I wanted to trigger script at the end of that specific conversation, my first thought would be to set a variable via an action on that conversation triggering. Then using a script to trigger 'OnConversationEnd' if it's true. Does that sound about right?

Again cheers for the help, you seem pretty invaluable to the people using this tool :).
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prefab Dialogue trigger System using script variables

Post by Tony Li »

You can add a script with an OnConversationEnd(Transform) method to the NPC. The Dialogue System will automatically call the method at the end of the conversation.
User avatar
rykaan
Posts: 75
Joined: Tue Feb 11, 2020 6:22 am

Re: Prefab Dialogue trigger System using script variables

Post by rykaan »

Cheers! That makes things pretty simple.

One final question, is there a reason why the C Sharp method would return false after the scene has been reloaded?

The method is triggering and the source bool is true, but the condition is showing as false, resulting in the conversation not triggering second time round.

Sorry to continually send these questions, I'm just not seeing where the scripts are failing to communicate correctly.
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prefab Dialogue trigger System using script variables

Post by Tony Li »

Are there any messages in the Console window?

Try adding some Debug.Log lines to your C# method to give you an idea of what it's doing, or set a debugger breakpoint and step through it.

You can also add a Lua Console component to your scene. Press ~+L to open the Lua Console, then enter:

Code: Select all

return CanSeePlayer("Bob")
to see the result of checking that function. Maybe "Bob" is no longer registered for some reason after reloading the scene.

Alternatively to using a Lua Console, you can use the Dialogue Editor's Watches tab. There's a "Run Lua Code" field at the bottom of editor on that page..
User avatar
rykaan
Posts: 75
Joined: Tue Feb 11, 2020 6:22 am

Re: Prefab Dialogue trigger System using script variables

Post by rykaan »

I finally figured out the problem (After a couple of hours this morning not the entire past week).

The character name that the LUA condition was looking for had at some point been added as a character name to the list of Actors in the database. Although it had been deleted I think it was defaulting to this old reference on reloading the scene. The issue didn't happen if I used new names that had never been added to the actor field.

At least this is what I think was happening. If so how do I delete any past reference to these names from that database? Since they aren't in the Actor list any more but are still affecting scripts.

If I am off on what I think is the cause then let me know, but using new names did fix the issue.

Cheers,
Rob
User avatar
Tony Li
Posts: 22055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Prefab Dialogue trigger System using script variables

Post by Tony Li »

Hi Rob,

You can use the Dialogue Editor's Global Search & Replace feature, available on the Database tab.
Post Reply