Dialogue trigger when hitting a certain value

Announcements, support questions, and discussion for the Dialogue System.
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Dialogue trigger when hitting a certain value

Post by simplepleasuresdxy »

Hi there,

I was hoping to inquire on what I could do about setting a dialogue trigger to activate after 7 clicks of something. I was thinking each click added to an unseen counter that would record how many times it was clicked and after 7 times, it would activate a dialogue, but only activate it once.

How could I go about that?
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue trigger when hitting a certain value

Post by Tony Li »

Hi,

Here's an example scene:

DS_Click7TimesExample_2023-07-26.unitypackage

To set it up:

1. In the Dialogue Editor window, add a Number variable. I called mine "TimesClicked":
addVariable.png
addVariable.png (18.71 KiB) Viewed 503 times

2. For my clickable object, I created a UI Button. I added two Dialogue System Triggers to this Button. They're both set to "OnUse". Then I configured the Button's OnClick() event to call both Dialogue System Triggers' OnUse() methods in order:
clickableDialogueSystemTriggers.png
clickableDialogueSystemTriggers.png (70.14 KiB) Viewed 503 times

3. The first Dialogue System Trigger increments the TimesClicked variable:
dsTrigger1.png
dsTrigger1.png (49.78 KiB) Viewed 503 times

4. The second Dialogue System Trigger has a condition that checks if TimesClicked is 7. If so, it plays a conversation:
dsTrigger2.png
dsTrigger2.png (76.92 KiB) Viewed 503 times
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Dialogue trigger when hitting a certain value

Post by simplepleasuresdxy »

This is awesome. Thank you!

Is there a way to link a unity variable to this in a similar way? I have a mechanic where players find runes and scanning them increases rune power by increments of one and i was envisioning a moment where a dialogue pops up after they get 3 magic power or something that tells them they can activate magic mode
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue trigger when hitting a certain value

Post by Tony Li »

simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Dialogue trigger when hitting a certain value

Post by simplepleasuresdxy »

so ... looking through my code, I don't have a button per say. All I have is a game object with a box collider attached to it where by clicking in that box collider activates an animation ...

how do I do the clicking set up without using a button?
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue trigger when hitting a certain value

Post by Tony Li »

Hi,

Does your GameObject have a script that handles the mouse click? If so, does it have a method named OnMouseDown() or OnMouseUp()? Either of these methods are a common way to handle mouse clicks.

If the script has either of these methods, then add this line of code in the top part of your script where you declare variables:

Code: Select all

public class AnExampleScript : MonoBehaviour
{
    public UnityEngine.Events.UnityEvent onClick; //<--- ADD THIS LINE!

Let's assume the script has an OnMouseDown() method. At the bottom of the OnMouseDown() method, add this line:

Code: Select all

void OnMouseDown()
{
    // (I'm guessing at how the animation is activated. This is just an example.)
    GetComponent<Animator>().Play("Some Animation");
    
    onClick.Invoke(); //<--- ADD THIS LINE!
}
This will add an OnClick() UnityEvent to the Inspector. You can configure it similarly to the UI Button's OnClick() UnityEvent above.
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Dialogue trigger when hitting a certain value

Post by simplepleasuresdxy »

Image

This is how it activates the animation
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue trigger when hitting a certain value

Post by Tony Li »

Hi,

Below the "[SerializeField] private string animationName;" line, add this line:

Code: Select all

[SerializeField] private string animationName;
 [SerializeField] private UnityEngine.Events.UnityEvent onClick; //<-- ADD THIS LINE
In the OnTirggerActivate() method, add this line to the end:

Code: Select all

public override void OnTriggerActivate()
{
    ...
    animationCoroutine = StartCoroutine(IResetToIdleState());
    onClick.Invoke(); //<-- ADD THIS LINE
}
(I put "..." in place of the rest of the code of that method since I didn't want to type it all out.)
simplepleasuresdxy
Posts: 73
Joined: Tue Jul 25, 2023 1:34 am

Re: Dialogue trigger when hitting a certain value

Post by simplepleasuresdxy »

Okay. Alright. I have this code for multiple objects. Do i have to make a seperate code so that other objects dont "invoke" it or do i specify that this object invokes something?

How does this additional code work with what younwere saying. I feel like im really close to understanding this
User avatar
Tony Li
Posts: 22124
Joined: Thu Jul 18, 2013 1:27 pm

Re: Dialogue trigger when hitting a certain value

Post by Tony Li »

Hi,

The code changes I suggested above add an OnClick() UnityEvent that you can configure in the inspector. If you don't assign anything to the OnClick() UnityEvent, it'll be just like you never added the code to the script in the first place.
Post Reply