Accessing different script

Announcements, support questions, and discussion for the Dialogue System.
phatality123
Posts: 8
Joined: Tue Dec 23, 2014 9:22 am

Accessing different script

Post by phatality123 »

how can i access a different script or script variables from a certain node in the dialogue manager, if its possible? thanks
phatality123
Posts: 8
Joined: Tue Dec 23, 2014 9:22 am

Accessing different script

Post by phatality123 »

And how would I access them if they're on a separate gameObject?
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Accessing different script

Post by Tony Li »

Hi,



Here are some methods to accomplish what you want. First, a little background:



Each dialogue node has Conditions and Script fields. You can reference Lua variables in these fields. For example, say you've defined a Lua variable named choseRedPill.  In the node where the player chooses the red pill, you can set the Script to:

Variable[choseRedPill] = true

The Dialogue Manager GameObject (technically the DialogueSystemController script on it) maintains a global Lua environment that any script can access.







METHOD 1:



Your other scripts can get and set Lua variables. (See How to Use Lua in Scripts.) For example, say you have a script that keeps track of the current power level on a GameObject named Force Field. You could keep a Lua variable in sync with the current power level like this:

// This coroutine on Force Field recharges its power level every second:

IEnumerator RechargePowerLevel() {

while (currentLevel

Then say you have a dialogue node that's only available when the power level is greater than 50. Set the node's Conditions field to:

Variable[powerLevel] > 50





METHOD 2:



You can register your own C# functions with Lua. (See Registering Functions.)



For example, add something like this to your Force Field script:

public class ForceField : MonoBehaviour {



int currentLevel = 100;



void Start() {

Lua.RegisterFunction("GetPowerLevel", this, typeof(ForceField).GetMethod("GetPowerLevel"));

}



public double GetPowerLevel() { // Lua works exclusively with doubles

return currentLevel; // when dealing with numbers.

}

}

This registers a function named GetPowerLevel() with Lua. Then, in your Conditions field, you can use:

GetPowerLevel() > 50





METHOD 3:



If  you only want to run a method on another script, you can avoid the Lua environment entirely and just use the SendMessage() sequencer command. Say you have an NPC GameObject named Fred that has a script named Nameplate. This script has a method SetTitle(string title):

public void SetTitle(string title) {

guiText.text = title + " " + name;

}



You can call this method by setting the Sequence field to:

SendMessage(SetTitle, Emperor, Fred)
phatality123
Posts: 8
Joined: Tue Dec 23, 2014 9:22 am

Accessing different script

Post by phatality123 »

Oh ok I think I understand. Thanks for the in-depth explanation :D.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Accessing different script

Post by Tony Li »

This is probably the most complicated aspect of the Dialogue System, so if you have any questions, don't hesitate to ask. No need to bang your head against the keyboard if I can help with anything. :-)
phatality123
Posts: 8
Joined: Tue Dec 23, 2014 9:22 am

Accessing different script

Post by phatality123 »

:D Thanks. Yeah I was trying to figure it out for awhile but couldn't come to a solution xD. Actually I do have one more question; is there any way you can change the color of certain words in a conversation node? Been wondering how to do that.
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Accessing different script

Post by Tony Li »

On the Dialogue Manager GameObject, tick Rich Text Emphases, then wrap the words in [em] tags. For example:



Dialogue Text: Hulk [em1]SMASH![/em1]



You can define the values for em1 - em4 in the Dialogue Editor on the Database tab.



Or you can just put rich text codes directly in your Dialogue Text:



Dialogue Text: Hulk <b><color=red>SMASH!</color></b>



Either way, your GUI system should support rich text codes. Unity GUI does, but you need to make sure you've ticked the Rich Text checkbox in the GUI Skin's style.
Aelcyx
Posts: 9
Joined: Wed Jan 19, 2022 1:50 am

Re: Accessing different script

Post by Aelcyx »

I have a similar issue in this vein, but can't seem to get these solutions to work. I have multiple instances of a prefab in my scene. They each have a C# script on them that takes in different parameters. How would I go about accessing Lua functions based on these parameters? That is, can I register Lua functions for each instance even if the registered Lua functions have the same name? Or should the names be different for each instance? If so, how to call them? Thanks!
User avatar
Tony Li
Posts: 21055
Joined: Thu Jul 18, 2013 1:27 pm

Re: Accessing different script

Post by Tony Li »

Hi,

Lua functions are registered globally. Two solutions:

1. Register differently-named Lua functions such as HealAdam(), HealBob(), HealCindy()

2. Or provide the actor/GameObject name to the function: Heal("Adam"), Heal("Bob"), Heal("Cindy")

If you use (2), you can use SequencerTools.FindSpecifier("Adam") to find the character Adam. It will first check if any GameObjects have Dialogue Actor components that have registered themselves as representing the actor "Adam". If not, it will look for a GameObject named "Adam".

An advantage of (2) is that you can use Variable["Actor"] and Variable["Conversant"] in conversations. For example, if you want to heal the conversation's conversant (typically the NPC involved in the conversation), use: Heal(Variable["Conversant"])
Aelcyx
Posts: 9
Joined: Wed Jan 19, 2022 1:50 am

Re: Accessing different script

Post by Aelcyx »

Thanks! That made a lot of sense and worked!

I found that I had to change the actor name to match the game object name. Is there a way within Lua, perhaps with a Variable["varname"] command? I used Variable["Actor"] and made sure it matched the game object. If I can do it without changing the name, that'd be great, but if not, I'll make it work. :)
Post Reply