If Then Variable in Node Script

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
BWLGeorge
Posts: 41
Joined: Sun Jun 19, 2022 10:10 pm

If Then Variable in Node Script

Post by BWLGeorge »

Howdy!

I'm trying to do some Icarian complicated branching dialogue stuff, and I'm just trying to make sure I understand the Conditional command or not.

Basically the idea is that the dialogue changes based on the order in which the player learned information, and I'm trying to use a number variable that will change based on that.

In my example code here, 0 means the player hasn't talked to either A or B. Talking to character A sets it to 1, talking to character B sets it to 2. But I want to make it so that if you have talked to character B before talking to character A, it will be set to 3 (and subsequently if you talked to A before talking to B, it will be set to 4.) Does this code look like it should do the job?

Character A conversation node:
Conditional("Variable['Sanctuarywasfoulplay'] == 0",
Variable["Sanctuarywasfoulplay"] = 1;
Conditional("Variable['Sanctuarywasfoulplay'] == 2",
Variable["Sanctuarywasfoulplay"] = 3;

And then the inverse for Character B:
Conditional("Variable['Sanctuarywasfoulplay'] == 0",
Variable["Sanctuarywasfoulplay"] = 2;
Conditional("Variable['Sanctuarywasfoulplay'] == 1",
Variable["Sanctuarywasfoulplay"] = 4;
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: If Then Variable in Node Script

Post by Tony Li »

Hi,

The Conditional() Lua function is usually used inside a [lua(code)] markup tag in Dialogue Text, although you can use it anywhere Lua is accepted, such as Conditions and Script fields.

However, Conditional() might not be what you want. Conditional(a, "b") evaluates a. If it's true, it returns b. Otherwise it returns a blank string. It doesn't set any values.

Instead, you can use one or both of these approaches:

1. In the dialogue entry's Script field, write an if-elseif-else expression like:

Code: Select all

if (Variable['Sanctuarywasfoulplay'] == 0) then
     Variable["Sanctuarywasfoulplay"] = 1;
elseif ("Variable['Sanctuarywasfoulplay'] == 2) then
    Variable["Sanctuarywasfoulplay"] = 3;
end

2. And/or use two separate dialogue entry nodes, linked from the same originating node:

Node 1:
  • Conditions: Variable['Sanctuarywasfoulplay'] == 0
  • Script: Variable['Sanctuarywasfoulplay'] = 1
Node 2:
  • Conditions: Variable['Sanctuarywasfoulplay'] == 2
  • Script: Variable['Sanctuarywasfoulplay'] = 3
You'll probably want a third node in case Variable['Sanctuarywasfoulplay'] is neither 0 nor 2. If you make this the third node in the originating node's Links To: list, it will only be used if the other nodes' Conditions are false.
BWLGeorge
Posts: 41
Joined: Sun Jun 19, 2022 10:10 pm

Re: If Then Variable in Node Script

Post by BWLGeorge »

Ahhh, that makes a lot of sense. That first method was what I was putting together originally until I stumbled on the Conditional function.

Thanks for the tips!
User avatar
Tony Li
Posts: 22143
Joined: Thu Jul 18, 2013 1:27 pm

Re: If Then Variable in Node Script

Post by Tony Li »

Happy to help!
Post Reply