Can I enable a Dialogue Trigger in a C# Script?

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
cruddish
Posts: 4
Joined: Mon Dec 02, 2024 3:20 pm

Can I enable a Dialogue Trigger in a C# Script?

Post by cruddish »

Hi there, I'm currently trying to enable a conversation to trigger only when a certain variable in a C# script is true. Here is my current script, where PlayerRotation is the player object, SplineMoving is the movement script, and the Dialogue Trigger is on the object where this script is attached. I have the Dialogue Trigger set to "On Enable" but that just starts the conversation on awake. Is this something I would have to do through the Lua conditions?

Code: Select all

using System.Collections;
using System.Collections.Generic;
using PixelCrushers.DialogueSystem;
using UnityEngine;

public class DialoguePosTrigger : MonoBehaviour
{
    public bool inDialogue;
    public SplineMoving splineMoving;
    public DialogueSystemTrigger dialogueTrigger;

    // Start is called before the first frame update
    void Start()
    {
        dialogueTrigger = GetComponent<DialogueSystemTrigger>();
        splineMoving = GameObject.Find("PlayerRotation").GetComponentInChildren<SplineMoving>();
        inDialogue = false;
    }

    // Update is called once per frame
    void Update()
    {
        inDialogue = splineMoving.inDialogue;
        if (splineMoving.inDialogue)
        {
            dialogueTrigger.gameObject.SetActive(true);
        }
    }
}
User avatar
Tony Li
Posts: 23256
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can I enable a Dialogue Trigger in a C# Script?

Post by Tony Li »

Hi,

Do you want the Dialogue System Trigger to start as soon as the variable becomes true, or do you want some other event to try to start the Dialogue System Trigger but it won't start if the variable is false?

For the former (start when variable becomes true), change your C# variable to a C# property. Example: (where "something" is your variable/property)

Code: Select all

public DialogueSystemTrigger dsTrigger; //<--Assign in inspector; set Trigger to OnUse.
private bool something = false;
public bool Something
{
    get { return something; }
    set { something = value; if (value == true) dsTrigger.OnUse(); }
}
For the latter, create a C# function and register it with Lua. Then check that C# function in the Dialogue System Trigger's Conditions > Lua Conditions. Example:

Code: Select all

private bool something = false;

bool IsSomethingTrue() { return something; }

void Awake() { Lua.RegisterFunction(nameof(IsSomethingTrue), this, SymbolExtensions.GetMethodInfo(() => IsSomethingTrue())); }
Then set the Dialogue System Trigger's Conditions > Lua Conditions to: IsSomethingTrue()
Post Reply