Page 1 of 1

Using a custom script

Posted: Thu Nov 03, 2016 9:51 am
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.

Re: Using a custom script

Posted: Thu Nov 03, 2016 3:37 pm
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.

Re: Using a custom script

Posted: Fri Nov 04, 2016 11:37 am
by Trex
Fantastic! This worked.

Thank you again!

Re: Using a custom script

Posted: Fri Nov 04, 2016 12:01 pm
by Tony Li
Glad to help!