Stormworks: Build and Rescue

Stormworks: Build and Rescue

Cal99 Apr 20, 2020 @ 2:22pm
LUA: Not Equal To, programming help
What I'm trying to get it to do seems really simple but just aint working, how do you use a 'not equal to' operator?

This is what I've got so far

if( Value (not equal to operator) ABCD or 1234 )
then
1
2
3
end

I've tried != and ~= but they don't work

Thanks for any help in advance
Originally posted by Arnstein86:
Easy method is just make:

If codevalue = ABCD
then xyz
Elseif codevalue = 1234
then 123
Else something
end


'if( x ~= ABCD and x ~= EFGH )' is the same as 'if not (x == ABCD or x == EFGH)'
You have to consider the or-argument isn't what you read out in plain English.
See more here if you like:

De Morgan's Laws[en.wikipedia.org]
< >
Showing 1-12 of 12 comments
Arnstein86 Apr 20, 2020 @ 3:02pm 
'~=' should be the correct operator.

Are you wanting the If condition to be True when the value is not equal to either of your variables?

Something in the lines of this, maybe?

ABCD = 1 EFGH = 2 x = 3 if( x ~= ABCD and x ~= EFGH ) then print("Value is not equal" ) else print("Value is equal" ) end

Not a LUA-expert, but some coding experience.
Ra-Ra-Rasputin Apr 20, 2020 @ 3:07pm 
I actually have not used a unequal operator in my LUA scripts for this game, but i believe while the "natural" syntax of LUA is ~= (as Arnstein86 pointed out) that is quite annoying to write for vast majority of the world, != should work in most LUA interpreters.
Last edited by Ra-Ra-Rasputin; Apr 20, 2020 @ 3:08pm
Cal99 Apr 20, 2020 @ 3:16pm 
Ye, I want the if statement to be true as long as x is not equal to the other values but if possible it be and "or" instead of "and".

currently I've got 2 other if statements above the problem one,

if(codeValue == ABCD)

then
XYZ

end

if(codeValue == 1234)

then
123

end

if(codeValue ~= ABCD or 1234 )

then
"NOT WORK PROPERLY"

end

Probably not the most optimised way of doing it I know but I don't know any other way.
And I thought the same Rasputin, when you use an operator in the script it colours it purple and this doesn't happen with "!="
Last edited by Cal99; Apr 20, 2020 @ 3:19pm
Ra-Ra-Rasputin Apr 20, 2020 @ 3:19pm 
LUA really makes me miss the Java-like syntax of C#.
if (codeValue == 1234) then foo(x) end

vs.

if (codeValue == 1234) foo(x);

So much excess writing :lunar2019deadpanpig:
Last edited by Ra-Ra-Rasputin; Apr 20, 2020 @ 3:19pm
The author of this thread has indicated that this post answers the original topic.
Arnstein86 Apr 20, 2020 @ 3:25pm 
Easy method is just make:

If codevalue = ABCD
then xyz
Elseif codevalue = 1234
then 123
Else something
end


'if( x ~= ABCD and x ~= EFGH )' is the same as 'if not (x == ABCD or x == EFGH)'
You have to consider the or-argument isn't what you read out in plain English.
See more here if you like:

De Morgan's Laws[en.wikipedia.org]
Last edited by Arnstein86; Apr 20, 2020 @ 3:32pm
Arnstein86 Apr 20, 2020 @ 3:30pm 
Originally posted by Ra-Ra-Rasputin:
I actually have not used a unequal operator in my LUA scripts for this game, but i believe while the "natural" syntax of LUA is ~= (as Arnstein86 pointed out) that is quite annoying to write for vast majority of the world, != should work in most LUA interpreters.


Yeah, don't like the '~='-syntax myself. It is very unintuitive on the keyboard :P
Cal99 Apr 20, 2020 @ 3:38pm 
Originally posted by Arnstein86:
Easy method is just make:

If codevalue = ABCD
then xyz
Elseif codevalue = 1234
then 123
Else something
end


'if( x ~= ABCD and x ~= EFGH )' is the same as 'if not (x == ABCD or x == EFGH)'
You have to consider the or-argument isn't what you read out in plain English.
See more here if you like:

De Morgan's Laws[en.wikipedia.org]

I don't entirely understand why this works but it does so thank you.

And also I originally thought ~= meant roughly equal to instead.
Last edited by Cal99; Apr 20, 2020 @ 3:40pm
Arnstein86 Apr 20, 2020 @ 3:43pm 
Originally posted by Shikigami99:
I don't entirely understand why this works but it does so thank you


Nice! Glad you got it working.
Jorg Hammond Apr 20, 2020 @ 3:52pm 
If you have some leisure time and want to understand, I'd suggest you check this resource: https://stackoverflow.com/questions/11658975/not-equal-to-this-or-that-in-lua

To sum it up, you were telling the program to evaluate
- x ~= ABCD (true or false depending on the situation)
or
- 1234 (which if it is not nil is true)

(true) or true is : true, (false) or true is : also true, thus your condition was always true, hence the program was not behaving like you expected it to.
Cal99 Apr 20, 2020 @ 3:55pm 
One last thing, can you stack an elseif

EG:

If codevalue = ABCD
then xyz
Elseif codevalue = 1234
then 123
Elseif codevalue >= XYZ
then
Elseif codevalue <= 9876
then
Else something
end

Would this work?

Arnstein86 Apr 20, 2020 @ 3:58pm 
Originally posted by Shikigami99:
One last thing, can you stack an elseif

EG:

If codevalue = ABCD
then xyz
Elseif codevalue = 1234
then 123
Elseif codevalue >= XYZ
then
Elseif codevalue <= 9876
then
Else something
end

Would this work?

Yes, but it will stop where the expression is true and not test more expressions.
(EDIT: Which is a good thing! Writing multiple IF's is forcing the script to check each statement. ELSEIF's can improve performance)

See syntax:

LUA IF ELSEIF ELSE[www.tutorialspoint.com]
Last edited by Arnstein86; Apr 20, 2020 @ 4:19pm
Arnstein86 Apr 20, 2020 @ 4:02pm 
Originally posted by Rashtek Frites:
If you have some leisure time and want to understand, I'd suggest you check this resource: https://stackoverflow.com/questions/11658975/not-equal-to-this-or-that-in-lua

To sum it up, you were telling the program to evaluate
- x ~= ABCD (true or false depending on the situation)
or
- 1234 (which if it is not nil is true)

(true) or true is : true, (false) or true is : also true, thus your condition was always true, hence the program was not behaving like you expected it to.

Exactly! As I said the OR-operator is not what you read out in plain English and the OR-operator comes last in the precedence - it is evaluated last.

But that guy explained it better than I can :)
Last edited by Arnstein86; Apr 20, 2020 @ 4:06pm
< >
Showing 1-12 of 12 comments
Per page: 1530 50

Date Posted: Apr 20, 2020 @ 2:22pm
Posts: 12