Stormworks: Build and Rescue

Stormworks: Build and Rescue

Cal99 2020년 4월 20일 오후 2시 22분
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
첫 게시자: 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]
< >
전체 댓글 12개 중 1~12개 표시 중
Arnstein86 2020년 4월 20일 오후 3시 02분 
'~=' 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 2020년 4월 20일 오후 3시 07분 
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.
Ra-Ra-Rasputin 님이 마지막으로 수정; 2020년 4월 20일 오후 3시 08분
Cal99 2020년 4월 20일 오후 3시 16분 
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 "!="
Cal99 님이 마지막으로 수정; 2020년 4월 20일 오후 3시 19분
Ra-Ra-Rasputin 2020년 4월 20일 오후 3시 19분 
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:
Ra-Ra-Rasputin 님이 마지막으로 수정; 2020년 4월 20일 오후 3시 19분
글타래 작성자가 이 게시물을 해당 주제의 답변으로 채택하였습니다.
Arnstein86 2020년 4월 20일 오후 3시 25분 
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]
Arnstein86 님이 마지막으로 수정; 2020년 4월 20일 오후 3시 32분
Arnstein86 2020년 4월 20일 오후 3시 30분 
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 2020년 4월 20일 오후 3시 38분 
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.
Cal99 님이 마지막으로 수정; 2020년 4월 20일 오후 3시 40분
Arnstein86 2020년 4월 20일 오후 3시 43분 
Shikigami99님이 먼저 게시:
I don't entirely understand why this works but it does so thank you


Nice! Glad you got it working.
Jorg Hammond 2020년 4월 20일 오후 3시 52분 
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 2020년 4월 20일 오후 3시 55분 
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 2020년 4월 20일 오후 3시 58분 
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]
Arnstein86 님이 마지막으로 수정; 2020년 4월 20일 오후 4시 19분
Arnstein86 2020년 4월 20일 오후 4시 02분 
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 :)
Arnstein86 님이 마지막으로 수정; 2020년 4월 20일 오후 4시 06분
< >
전체 댓글 12개 중 1~12개 표시 중
페이지당 표시 개수: 1530 50

게시된 날짜: 2020년 4월 20일 오후 2시 22분
게시글: 12