Script with if/then exception

Announcements, support questions, and discussion for the Dialogue System.
Post Reply
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Script with if/then exception

Post 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?
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Script with if/then exception

Post 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"])
Deadcow
Posts: 28
Joined: Tue Apr 30, 2019 8:21 am

Re: Script with if/then exception

Post by Deadcow »

Thanks for the quick response! 😊
User avatar
Tony Li
Posts: 22142
Joined: Thu Jul 18, 2013 1:27 pm

Re: Script with if/then exception

Post by Tony Li »

Glad to help!
Post Reply