Using condition in dialogue text

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Faydaen
Posts: 1
Joined: Sat Jul 09, 2022 12:50 am

Using condition in dialogue text

Post by Faydaen »

Hello, can you help me?

I want to use "if .. then" statement in nodes

For example I have variable "number_of_pirates" and I want to change NPS replica depending on it

And I have this lua code

Code: Select all

[lua(
if (Variable["number_of_pirates"] > 1) 
    then 
        "there are " .. Variable["number_of_pirates"] .. " more pirates left" 
    else
        "there is one more pirate left"
    end
)]
In theory this code must generate phrase like: "there are 2 more pirates left"

I wrote it in one line
Image

But I get nil instead.
Image

And no errors or warnings in console

P.S.

I know that I can use 2 nodes with appropriate conditions instead, but what if it will be more complicated logic?
User avatar
Tony Li
Posts: 21977
Joined: Thu Jul 18, 2013 1:27 pm

Re: Using condition in dialogue text

Post by Tony Li »

Hi,

Your [lua(code)] should return a value. You could write it as a function, or use the built-in Conditional() function. Conditional() takes two string parameters: a condition and the text to show if the condition is true. So you could do something like:

Code: Select all

[lua(Conditional("Variable['number_of_pirates'] > 1", "there are [var=number_of_pirates] more pirates left"))]
[lua(Conditional("Variable['number_of_pirates'] == 1", "there is one more pirate left"))]
Or you could Lua's ternary operator syntax: "condition and true-value or false-value which is more compact":

Code: Select all

lua((Variable['number_of_pirates'] > 1) and "there are [var=number_of_pirates] more pirates left" or "there is one more pirate left")]
Post Reply