How to add an Action to a DialogueSystemTrigger componenet threw scripting

Announcements, support questions, and discussion for the Dialogue System.
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

Ok I have made some progress, If everything is set up on the scene before clicking play it works as intended, but if i'm adding the component via script, than it doesnt, work... but the thing is i really need to set those things via script....
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by Tony Li »

What is the script code you're using?
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

Code: Select all

public class ActorManager : MonoBehaviour
{
    [SerializeField] private int defaultUsableDistance;
    [SerializeField] private Vector3 defaultDialoguePanelOffset;
    private DialogueSystemController dialogueSystemController;
    [SerializeField] private GameObject dialogueInteractorVR;
    private void Awake()
    {
        dialogueSystemController = FindObjectOfType<DialogueSystemController>();
        var player = FindObjectOfType<AutoHandPlayer>();
        
        foreach (var dialogueActor in FindObjectsOfType<DialogueActor>().Where(ActorHasDialogue))
        {
            dialogueActor.actor = dialogueActor.persistentDataName;
            //Add some Components (if not already on the object) with default value else we assume it is already on the object,
            //set with values that we don't want to change.
            if (!dialogueActor.gameObject.TryGetComponent<DialogueSystemTrigger>(out var dialogueSystemTrigger))
            {
                dialogueSystemTrigger = dialogueActor.gameObject.AddComponent<PixelCrushers.DialogueSystem.Wrappers.DialogueSystemTrigger>();
                dialogueSystemTrigger.trigger = DialogueSystemTriggerEvent.OnUse;
                dialogueSystemTrigger.selectedDatabase = dialogueSystemController.initialDatabase;
                dialogueSystemTrigger.conversation = dialogueActor.persistentDataName;
                dialogueSystemTrigger.maxConversationDistance = defaultUsableDistance;
                dialogueSystemTrigger.conversationConversant = player.transform;
                dialogueSystemTrigger.conversationActor = dialogueSystemTrigger.transform;
                dialogueActor.actor = dialogueActor.persistentDataName;
            }

            if (!dialogueActor.gameObject.TryGetComponent<Usable>(out var usable))
            {
                usable = dialogueSystemTrigger.gameObject.AddComponent<Usable>();
                usable.maxUseDistance = defaultUsableDistance;
            }

            if (!XRSettings.enabled) continue;
           
            if (!dialogueActor.gameObject.TryGetComponent<MoveDialoguePanel>(out var panel))
            {
                panel = dialogueSystemTrigger.gameObject.AddComponent<MoveDialoguePanel>();
                panel.offset = defaultDialoguePanelOffset;
            }
            
            if (!dialogueActor.gameObject.TryGetComponent<DialogueSystemInteractable>(out var interactable))
            {
                Instantiate(dialogueInteractorVR, dialogueActor.gameObject.transform);
                dialogueActor.gameObject.AddComponent<DialogueSystemInteractable>();
            }
            
        }
    }
    
    private bool ActorHasDialogue(DialogueActor dialogueActor)
    {
        return dialogueSystemController.databaseManager.defaultDatabase.actors.Any(actor => dialogueActor.persistentDataName == actor.Name);
    }
}
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by Tony Li »

After that code runs, if you inspect the Dialogue System Trigger in the inspector, does it look correct?

Also, please feel free to send a reproduction project to tony (at) pixelcrushers.com if you'd like me to take a direct look.
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

yes it looks the same as if i set it manually.
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by Tony Li »

Try switching which GameObjects you assign to the conversationActor and conversationConversant.

If that doesn't help, please send me a reproduction project. If you can't send a repro project, let me know; I can put together an example scene.
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

does the dialogue manager need to save the trigger component in its initialisation somewhere? if thats the case maybe this is why its not working, because he dont know any of the actor?
User avatar
Tony Li
Posts: 21959
Joined: Thu Jul 18, 2013 1:27 pm

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by Tony Li »

No, it doesn't need to save or cache anything.
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

Well it would be hard to send the project since it's really heavy, that being said here are the steps and the goal if you want to test on your own why it is not working. I'm still gonna try and continue to look on my side, but if you have other idea on why it doesnt work, i really need a fix.

1. On a menu scene i make a selection, that selection will change the database i will use in the next scene (dialogue manager is already instanciated on the menu scene)

2. On the next scene, i have 10 actors, but depending on the selection made at the menu, it's not necessarly all of them that will have dialogue. numbers and which one may vary. This is why im running the script I sent before to change all actors configuration at runtime. For that, conversation title and actor name must be the same as the persistant data name to ensure evrything is link properly.
ChaChaCHAT
Posts: 24
Joined: Mon Aug 01, 2022 7:58 am

Re: How to add an Action to a DialogueSystemTrigger componenet threw scripting

Post by ChaChaCHAT »

I Also have those other script that may be usefull
This one make the dialogue panel spawn relative to the npc at the good place

Code: Select all

public class MoveDialoguePanel : MonoBehaviour
{
    public bool lookAtCamera = true;
    public bool flip = false;
    public Vector3 offset;
    
    [Preserve]
    private void OnConversationStart(Transform actor)
    {
        if (Camera.main == null) return;
        
        // Get reference to dialogue panel's canvas:
        var dialoguePanel = DialogueManager.standardDialogueUI.conversationUIElements.mainPanel;
        if (dialoguePanel == null) return;
        var dialoguePanelCanvas = dialoguePanel.GetComponentInParent<Canvas>();
        if (dialoguePanelCanvas == null) return;

        // Face the player:
        if (lookAtCamera)
        {
            var lookAtPosition = Camera.main.transform.position;
            lookAtPosition.y = transform.position.y; 
            transform.LookAt(lookAtPosition);
        }

        // May need to flip based on NPC's rotation (i.e., if NPC's model rotation is backward):
        if (flip)
        {
            var scale = dialoguePanelCanvas.transform.localScale;
            scale.x = -Mathf.Abs(scale.x);
            dialoguePanelCanvas.transform.localScale = scale;
        }

        // Move dialogue panel to offset from me:
        dialoguePanelCanvas.transform.rotation =  transform.rotation * Quaternion.Euler(0, 180, 0);
        dialoguePanelCanvas.transform.position = transform.position + transform.rotation * offset;
    }
}
and this one is the one i wrote for the talk button

Code: Select all

public class DialogueSystemInteractable : MonoBehaviour
{
    private Button buttonUI;
    private DialogueSystemTrigger trigger;
    private void Start()
    {
        buttonUI = GetComponentInChildren<Canvas>().gameObject.GetComponentInChildren<Button>();
        trigger = GetComponent<DialogueSystemTrigger>();
        
        buttonUI.onClick.AddListener(() =>
        {
            trigger.OnUse(transform);
        });
    }
}
Post Reply