Can I have a sequence/lua that modifies the subtitle text?

Announcements, support questions, and discussion for the Dialogue System.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

I've got a weird situation that may not be supported. In my game, you have limited energy. I want to have dialogue choices that when picked, spend energy. I also want the player to be informed that a certain choice will spend energy. The way they'll be informed is with an icon in the text. I've already figured out how to get the icon in the text using Text Mesh Pro. I know how to create a sequence that subtracts energy. Is there a way to make it so that every time I use this sequence, the subtitle text is modified to have the icon at the beginning or end? I'm trying to make it so it's difficult for me to forget one or the other.

Another thing I don't know how to do is run a sequence only if a response is picked, but not if it's displayed.
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by Tony Li »

Hi,
AoF wrote: Wed Jun 12, 2019 12:31 amI've got a weird situation that may not be supported. In my game, you have limited energy. I want to have dialogue choices that when picked, spend energy. I also want the player to be informed that a certain choice will spend energy. The way they'll be informed is with an icon in the text. I've already figured out how to get the icon in the text using Text Mesh Pro. I know how to create a sequence that subtracts energy. Is there a way to make it so that every time I use this sequence, the subtitle text is modified to have the icon at the beginning or end? I'm trying to make it so it's difficult for me to forget one or the other.
I'm not sure what you mean. Are you talking about a single dialogue node in which the icon sometimes appears at the beginning of the node's text and sometimes at the end of the text?

Maybe you can make use of [var=variable] tags in the text. This tag inserts a variable value.
(You can also use [lua(code)]. It's more flexible but also more unwieldy.)

For example, say you have these two variables:
  • IconFront: (blank string)
  • IconEnd: "<sprite=CostsEnergy>"
And your Dialogue Text is: "[var=IconFront]Fire the laser cannon.[var=IconEnd]"

At runtime, the Dialogue Text will be: "Fire the laser cannon.<sprite=CostsEnergy>"

Alternatively, you can add a script with an OnConversationLine(Subtitle) method to the Dialogue Manager or one of the participants. In the method, modify the subtitle's formattedText.text. For example, say dialogue nodes have a custom field named "Energy Cost":

Code: Select all

void OnConversationLine(Subtitle subtitle)
{
    var energyCost = Field.LookupInt(subtitle.dialogueEntry.fields, "Energy Cost");
    if (energyCost > 0)
    {
        subtitle.formattedText.text += "<sprite=Energy> (" + energyCost + ")";
    }
}
AoF wrote: Wed Jun 12, 2019 12:31 amAnother thing I don't know how to do is run a sequence only if a response is picked, but not if it's displayed.
If I understand you correctly, the Dialogue System already works that way. If a dialogue entry node is assigned to the player and appears in a response menu, its sequence will only run if the player picks the response.

If the player dialogue entry node is configured to bypass the response menu -- either because it's the only response and the Always Force Response Menu is unticked, or its text contains the [auto] tag -- then its sequence will run as soon as the conversation gets to that node.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

OK so what I was looking for was a situation where there are 3 response choices and one of them costs energy. In the one that costs energy, I'd like to do something just like subtitle.formattedText.text += "<sprite=Energy> (" + energyCost + ")"; I think every action will always cost 1 energy.

It's not clear to me if I can access subtitle.formattedText.text if I'm showing a response, nor how to set a field.

Let me show you a screenshot of what I'd like to do:

But, I only want to have to remember to put a sequence/script on the node to put that symbol in the dialogue text, I don't want to have to remember to include a sequence/script AND remember to include the <sprite> in the text because I know I'd eventually forget one or the other.
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by Tony Li »

You can add a script with an OnConversationResponseMenu() method to the Dialogue Manager. It receives an array of Responses. Each Response has a formattedText property that you can change. For example:

Code: Select all

void OnConversationResponseMenu(Response[] responses)
 {
     foreach (var response in responses)
     {
         // Look up the response's cost:
         var cost = Field.LookupInt(response.destinationEntry.fields, "Cost");
         
         // If it has a cost, add the sprite info to the text:
         if (cost > 0)
         {
             response.formattedText.text = "<sprite=Energy> (" + cost + ") " + response.formattedText.text;
         }
     }
 }
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

Cool, but where can I learn more about fields and how to set them?
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by Tony Li »

In the scripting API reference.

Actors, Items, Locations, Variables, and Conversations are all subclasses of Asset. (If you follow that link, the top of the page has a picture that shows Asset and its subclasses.) The Asset class has a List<Field> and some helper methods to read field values such as Asset.LookupInt. These helper methods call static methods in the Field class, such as Field.LookupInt.

DialogueEntry is not a subclass of Asset. (The Dialogue System uses the same data structure as a third party dialogue program called Chat Mapper, and that's just the way it is in Chat Mapper.) This means DialogueEntry doesn't inherit Asset's helper methods to read field values. Instead, you'll need to directly use the Field class's static methods (such as Field.LookupInt).

This page has a general intro to scripting with the Dialogue System: Scripting.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

Great, thanks!
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

From that documentation, I know I can Field.SetValue(something, "Cost", something); somewhere, but I'm not sure where. The documentation hints at it, but it's around a section about creating my own database at runtime. Is that something I need to do in order to have this functionality?
  • If so, the documentation doesn't say where to put the code that it gives as an example (I guess it'd be on an empty game object?).
  • If not, the documentation doesn't say anything about using fields on an already existing database. Perhaps those attributes are a way of doing it, but I'm not confident that's what it's trying to communicate.
I also am unsure about the effect of adding a field. Does this add a row in the inspector when I click on an object? Is it completely in code and something you can't visualize?

What is a popup? Is that relevant?
User avatar
Tony Li
Posts: 22056
Joined: Thu Jul 18, 2013 1:27 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by Tony Li »

Hi,
AoF wrote: Thu Jun 13, 2019 1:37 amFrom that documentation, I know I can Field.SetValue(something, "Cost", something); somewhere, but I'm not sure where. The documentation hints at it, but it's around a section about creating my own database at runtime. Is that something I need to do in order to have this functionality?
I just updated these pages in the manual:
  • Overview: Brief distinction between values in dialogue database vs. Lua.
  • Lua: Brief distinction between values in dialogue database vs. Lua, plus link to Scripting.
  • Scripting: Dialogue database fields and Lua values section.
The Scripting section Dialogue Database Fields and Lua Values should clarify a lot.

Briefly, treat the dialogue database as read-only. Use Field methods such as Field.LookupInt in any script to read its values. Use DialogueLua methods to change values at runtime and check the current runtime values.

AoF wrote: Thu Jun 13, 2019 1:37 amI also am unsure about the effect of adding a field. Does this add a row in the inspector when I click on an object? Is it completely in code and something you can't visualize?
You can add a field in custom editor code (i.e., design time, not runtime) or if you're creating an entirely new dialogue database, but in general at runtime you should use DialogueLua.
AoF wrote: Thu Jun 13, 2019 1:37 amWhat is a popup? Is that relevant?
Probably not. They make Dialogue System-related variables in your own scripts nicer to work with. For example, if you add the [ConversationPopup] attribute to a string variable, the Unity inspector will show a dropdown menu instead of a plain text entry field.

I really appreciate your feedback letting me know where the documentation can be supplemented. If any of the additions I made today cause more confusion than they resolve, or if you have other suggestions, please let me know.
AoF
Posts: 241
Joined: Sun May 12, 2019 8:36 pm

Re: Can I have a sequence/lua that modifies the subtitle text?

Post by AoF »

OK, I think I pieced together that the place to add custom fields is here: I was only able to do that because I remember reading that in the docs weeks ago.

Somewhere the documentation should say, "There are default fields like x on the actor and y on the conversation, but you can customize your own in the template tab of the dialogue editor." It might be worth showing an image like that in the Dialogue Database Fields and Lua Values section since that seems like half the story of what fields are.

Thanks!
Post Reply