Lua and C#

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
aallenfx
Posts: 4
Joined: Mon May 15, 2023 6:47 pm

Lua and C#

Post by aallenfx »

Hey,

I'm trying setup a dialogue tree that checks an external C# class.

For example the players choice executes code. I want to check if the player has an item in my c# class but communicate this to the dialogue system's current conversation.

What would the syntax be for checking a c# class? I haven't used Lua before so I'm trying to understand the specifics.

The "Yes" branch would perform a check. Ex, my class is called "DialogueItems" and a func called "Check Items" returns a bool value. How would you reference my Instance of a class and access that function to get the boolean value or any other primitive data types (int/float/etc)?

Then how would the condition be written to branch between true/false? I just have "Variable["HasItem"] == true" and "Variable["HasItem"] == false" as the two conditions for branching.

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

Re: Lua and C#

Post by Tony Li »

Hi,

Register your C# function with Lua:
Then use it in dialogue entry nodes' Conditions fields.
aallenfx
Posts: 4
Joined: Mon May 15, 2023 6:47 pm

Re: Lua and C#

Post by aallenfx »

Hey,

Awesome, that helped alot. My follow up question would be, is it possible to pause a branch then continue it once a C# condition is fulfilled?

The "Then hand them over" triggers a "Item Window" class which lets the player place in an item through the UI. Then it would execute either:
A) Excellent (Success)
B) I don't have time (Fail)

But those would only execute AFTER the Item Window class calls an event or notifies the dialogue system. Otherwise it should sit in the "Then hand them over" branch until it's told to continue.

Since the action is performed via UI and not a "response" it would be Actor to Actor branching instead of the usual actor -> conversant chain.

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

Re: Lua and C#

Post by Tony Li »

Lua functions run immediately and are expected to return immediately.

If you want to control timing, such as making the conversation wait, use sequencer commands. Custom sequencer commands are just as easy to set up as Lua functions. But you might not even need a custom sequencer command. Say your ItemWindow class is on a GameObject named "Inventory", and it has a method ShowWindow(). You can call this method using the built-in SendMessage() sequencer command, and then wait for a sequencer message from the window:

Example Sequence:

Code: Select all

SendMessage(ShowWindow,,Inventory);
WaitForMessage(ClosedWindow)
Example C#:

Code: Select all

public class InventoryManager : MonoBehaviour
{
    public void ShowWindow() { ... } // Assume it eventually calls Close().
    
    public void Close()
    {
        PixelCrushers.DialogueSystem.Sequencer.Message("ClosedWindow");
    }
}
Post Reply