Using a custom script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Trex
Posts: 4
Joined: Wed Oct 05, 2016 7:22 am

Using a custom script

Post by Trex »

Hello

What is the simplest way to just have a script I've written execute when a certain dialog line is spoken? For instance, is there a way I can just drag and drop a script into a dialog node, and it'll fire when the node i used?

For instance, when I click a node in the dialog manager, I see this...

Image

I though perhaps I could just drop a script into the Events box, but it doesn't let me select the function afterwards (like I would see when building a GUI).

The closest answer I could find is http://pixelcrushers.com/dialogue_syste ... erFunction, but I was wondering if there was a simpler one.
User avatar
Tony Li
Posts: 22655
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using a custom script

Post by Tony Li »

Hi,

Use the SendMessage() sequencer command. For example, say your script "test100" has a function named DoSomething(), and you've added the script to a GameObject named "MyGameObject". Set the dialogue entry's Sequence field to:

Code: Select all

SendMessage(DoSomething,,MyGameObject);
Delay({{end}})
The first line runs the DoSomething() function on the scripts on MyGameObject. Note the empty second argument (between the two commas). You can pass a string value there instead if you need to.

The second line delays the dialogue entry for a duration based on the length of the subtitle text. You only need to include it if you want to delay for that duration.


Here's an explanation of why you can't use the OnExecute() event the way your screenshot depicts: Since dialogue databases are asset files that exist independently of scenes, you can't assign a GameObject to a dialogue entry's OnExecute() event. You also can't assign a MonoBehaviour script itself to OnExecute() because they have to be added to a GameObject first. You can, however, create a ScriptableObject asset and assign it to OnExecute() because it will exist outside of the scene. It's easiest to create it in C#. For example:

Test200.cs

Code: Select all

using UnityEngine;

[CreateAssetMenu]
public class Test200 : ScriptableObject {
    public void DoSomething() {
        PixelCrushers.DialogueSystem.DialogueManager.ShowAlert("Hello!");
    }
}
After this script compiles, right-click in the Project view and select Create > Test200. This will create an asset file. Then assign that asset file to the OnExecute() event and select the DoSomething() method.
Trex
Posts: 4
Joined: Wed Oct 05, 2016 7:22 am

Re: Using a custom script

Post by Trex »

Fantastic! This worked.

Thank you again!
User avatar
Tony Li
Posts: 22655
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using a custom script

Post by Tony Li »

Glad to help!
Post Reply