Call custom script method from specific dialogue entry

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Call custom script method from specific dialogue entry

Post by joeylu »

Hi,
I have a custom script added to the Actor game object with a public method, something like

public void RunAction(GameObject go) {... do something}

I need this method to be called whenever some specific dialogue entry is played.
I tried to use OnExecute() but that event is very limited. I think I should use the lua script but I have no clue how to write such script, need some guidances, tks
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Call custom script method from specific dialogue entry

Post by joeylu »

I'm thinking to use the SendMessage() is the right way, but since the actor has many custom comportment scripts, how do I specific which script's mehtod?

SendMessage(RunAction, goName)
or maybe
SendMessage(CustomScript.RunAction, goName) ?
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Call custom script method from specific dialogue entry

Post by Tony Li »

Hi,

In version 2.2.6 and earlier, the usual way would be to use SendMessage() or write a custom sequencer command or register a C# methods as a Lua function.

However, with 2.2.7+, you can add a scene-based UnityEvent to a dialogue entry node. Then you can assign your scene object and call its script method.
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Call custom script method from specific dialogue entry

Post by joeylu »

Just updated to 2.2.7, right on time, lol
If I understand correctly (I see the change in the inspector for Add Scene Events), the OnExecute() becomes more flexible, is this what you have mentioned "Scene Based Unity Event"?
At least I'm able to drag the gameobject and call its method with parameters.

However, my issue is that the Game Object and the method's parameter (also a game object), both of them are instantiated in runtime, it is a clone of a prefab, is it still possible to assign it in the inspector? or I need to write a custom script to create the OnExecute() in runtime? if so, how do I add the OnExecute for a specific dialogue entry? tks
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Call custom script method from specific dialogue entry

Post by Tony Li »

You can write a custom script that accesses the cloned instance and calls the method on it. For example, if the prefab is instantiated by some kind of spawner script, that spawner script can retain a reference to the instance and call a method on it.

Then you'd hook up the dialogue entry node's OnExecute() event to the spawner, since it exists in the scene at design time.
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Call custom script method from specific dialogue entry

Post by joeylu »

Hi Tony, not quite sure I fully understand your solution.
Do you mean that I should add a game object to the Scene hierarchy, add a custom script to hold some variables? So whenever my prefabs got instantiated, it pass its reference to that game object.
Then in the dialogue entry inspector, I add that game object and its custom script method to the OnExecute event?
thanks
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Call custom script method from specific dialogue entry

Post by Tony Li »

Yes. What instantiates the prefab in the first place?
joeylu
Posts: 111
Joined: Sun May 17, 2020 1:07 pm

Re: Call custom script method from specific dialogue entry

Post by joeylu »

it works, haha, the in scene event rocks!!! tks for the update Tony
User avatar
Tony Li
Posts: 21676
Joined: Thu Jul 18, 2013 1:27 pm

Re: Call custom script method from specific dialogue entry

Post by Tony Li »

Glad to help! That's been a big request for a while. I think it's going to really open up possibilities for people.
Post Reply