Page 1 of 1

Lua and C#

Posted: Mon Sep 30, 2024 8:49 pm
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.


Re: Lua and C#

Posted: Mon Sep 30, 2024 9:13 pm
by Tony Li
Hi,

Register your C# function with Lua:
Then use it in dialogue entry nodes' Conditions fields.

Re: Lua and C#

Posted: Tue Oct 01, 2024 12:09 pm
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.


Re: Lua and C#

Posted: Tue Oct 01, 2024 3:24 pm
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");
    }
}