logical and/or

Post Reply
Meridian
Posts: 6
Joined: Thu Dec 10, 2009 11:57 pm

logical and/or

Post by Meridian »

I realize that lua treats these differently than what I'm used to, and I can't quite get my head around another way to accomplish what I want without what I expect them to do. I'm hardly an experienced coder, so simple language would be great.

The idea is to execute certain healing patterns if both of my character's legs are damaged. I want to use something like:

Code: Select all

if (affLegLeftDamage > 0) & (affLegRightDamage > 0) then
do stuff
elseif ...
Is there a way for me to approximate that with lua, or some equivalent statement that you can help me out with?

Thanks in advance.

A

User avatar
Jules
Posts: 118
Joined: Sun Oct 11, 2009 5:41 pm
Location: Plymouth State University - Sophomore

Re: logical and/or

Post by Jules »

You pretty much have it, actually. Lua's a pretty straight forward language to learn. You pretty much have the basic gist of what would be in Lua, but your formatting is off. The translation of your code is:

Code: Select all

if affLegLeftDamage > 0 and affLegLeftDamage > 0 then
       do stuff
elseif ...
& = and
No parenthesis around the conditions, and you're good!

Meridian
Posts: 6
Joined: Thu Dec 10, 2009 11:57 pm

Re: logical and/or

Post by Meridian »

So, to make sure I have this conceptually, despite lua's odd treatment of and/or, this works out all right. That would be because each statement equates to either a true or a false argument. If the first argument boils down to false/nil, the entire condition is false. If the second boils down to false/nil, the entire condition is false. Otherwise, it returns whatever the second argument is, which in this case would be true, and the contingent block of code executes... right?

User avatar
Vadi
Posts: 5035
Joined: Sat Mar 14, 2009 3:13 pm

Re: logical and/or

Post by Vadi »

See http://lua-users.org/wiki/ExpressionsTutorial for some good information.

Iocun
Posts: 174
Joined: Wed Dec 02, 2009 1:45 am

Re: logical and/or

Post by Iocun »

Meridian wrote:So, to make sure I have this conceptually, despite lua's odd treatment of and/or, this works out all right. That would be because each statement equates to either a true or a false argument. If the first argument boils down to false/nil, the entire condition is false. If the second boils down to false/nil, the entire condition is false. Otherwise, it returns whatever the second argument is, which in this case would be true, and the contingent block of code executes... right?
Yeah. Whenever you're dealing with booleans, Lua's and/or work as in any other language. Likewise, if you convert the result of any or/and expression to a boolean, you should get the same result as any or/and expression in another language. (The exception being the number 0, which is considered true in Lua).

So in most cases you can use Lua's logical expressions like in another language. They simply have some additional functionalities.

User avatar
Jules
Posts: 118
Joined: Sun Oct 11, 2009 5:41 pm
Location: Plymouth State University - Sophomore

Re: logical and/or

Post by Jules »

To add to what Iocun said, and to summarize Booleans in general for Lua...

If it's not False or Nil, it's True.

Code: Select all

something > something else
or

Code: Select all

something < something else
are true statements.

Code: Select all

something = 0
is also a true statement because "something" can still have a value of zero, and be able to run properly in your code. However, as is typical in most coding, we usually script in "0" to be false, but Lua doesn't see it this way, just like I said before.

True is the opposite of False/Nil, but False ~= Nil. False means simply that. "It's NOT True", "It's NOT On", "It's NOT THAT VALUE". Nil means "It doesn't exist". Nil can give you problems, especially when you use lists, because if you call something with a value of Nil, and the script that you are calling requires it to have a value other than Nil (i.e. 1, 2, 0, True), it will spit back an error saying that whatever you are calling doesn't exist. SO! General rule of thumb, if you want a Boolean, and something must be the opposite of True, use False, not Nil!

Hopefully you learned something, because I felt "supa smukin' fart" typing all that! 8-)

Post Reply