Page 1 of 1

Script with if/then exception

Posted: Wed Nov 18, 2020 1:44 pm
by Deadcow
We tried to make script in dialogue a bit more complex (and we don't have any lua experience) and got this error:

Dialogue System: Lua code 'if Variable["TrainStartSequence"] == 0 then Variable["TrainStartSequence"] = 1' threw exception 'Code has syntax errors:
Line 2, Col 1 : Failed to parse Letter of Name.
Line 2, Col 1 : Failed to parse Name of VarName.


Is it possible to use if/then statement in script field? And if it is possible, how to do it properly?

Re: Script with if/then exception

Posted: Wed Nov 18, 2020 2:12 pm
by Tony Li
Hi,

Lua is mostly like C#, but 'if' statements are a little different because that have to end with the 'end' keyword. Try this:

Code: Select all

if Variable["TrainStartSequence"] == 0 then Variable["TrainStartSequence"] = 1 end
Or, for better readability:

Code: Select all

if (Variable["TrainStartSequence"] == 0) then 
     Variable["TrainStartSequence"] = 1 
 end
Alternatively, you could use math.max() to guarantee that the variable is at least 1:

Code: Select all

Variable["TrainStartSequence"] = math.max(1, Variable["TrainStartSequence"])

Re: Script with if/then exception

Posted: Thu Nov 19, 2020 9:14 am
by Deadcow
Thanks for the quick response! 😊

Re: Script with if/then exception

Posted: Thu Nov 19, 2020 10:04 am
by Tony Li
Glad to help!