NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 22 Jun 2015 04:49 AM |
Understanding true/false returns.
Every condition must be either true or false. Regardless of what the condition is, Lua will either read one or the other, and nothing in between.
Lua will also count a condition as true if it sees a value that isn't a boolean, or false if it sees nil.
---------- a = 5 if a then print("a exists! It's value is: "..a) end ----------
The above example will fire the print function because not a boolean, but it exists.
Using the "not" operator.
---------- a = true if not a then print("Result 1") else print("Result 2") end ----------
In the above example, result 2 will be chosen, because the "not" operator flips any condition after it. If it's true, it will be read as false. If it's false, it will be read as true.
Using the "and" operator.
---------- if CONDITION_1 and CONDITION_2 then print("Both are true!") end ----------
In the above example, both CONDITION_1 and CONDITION_2 both must be true for the print to fire, not one or the other.
Using the "or" operator.
---------- if CONDITION_1 or CONDITION_2 then print("One is true!") end ----------
In the above example, either CONDITION_1 or CONDITION_2 must be true for the print to fire, not necessarily both.
The one-line if statement.
---------- apple = (CONDITION and RESULTING_VALUE_1 or RESULTING_VALUE_2) ----------
In the above example, "apple" is equal to RESULTING_VALUE_1 if CONDITION is true. Otherwise it's equal to RESULTING_VALUE_2.
This is an intro to cool tricks you can do with logic operators. Lua will see the brackets as either true or false. Inside the brackets, there's a comparison going on. Only one of the two conditions can be correct:
(CONDITION and RESULTING_VALUE_1)
or,
(RESULTING_VALUE_2)
If CONDITION is not true, the first choice is impossible, and so it defaults to the second. You can add more "or" operators for more conditions. |
|
|
| Report Abuse |
|
|
cntkillme
|
  |
| Joined: 07 Apr 2008 |
| Total Posts: 44956 |
|
|
| 22 Jun 2015 04:55 AM |
'In the above example, "apple" is equal to RESULTING_VALUE_1 if CONDITION is true. Otherwise it's equal to RESULTING_VALUE_2.'
Not completely true. Due to the lack of a real ternary operator (like ?: in C/JS/etc.), you get problems when you have something along the lines of:
x = condition and false or "no"
If you want x to be equal to false if the condition is not false or nil, you would think this'd work, but no because it's actually read as:
(condition and false) or "no" So when condition is true, it returns the second one (aka false). That leads to: (false or "no")
Which will always give you no, so really x = "no" regardless. |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 22 Jun 2015 04:57 AM |
| @cnt never knew that. Thanks for the clarification c: |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2015 05:09 AM |
@cnt
that is why you use other forms or data representation.
0 = false 1 = true
Or similar... |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 22 Jun 2015 05:16 AM |
| @cody genius! I've never considered that |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2015 05:25 AM |
In a lot of my scripts I have adorned...
TRUE = true FALSE = false KINKY = true
So that I may do whatever.
while KINKY do
wait() end |
|
|
| Report Abuse |
|
|
NotAshley
|
  |
| Joined: 16 Jan 2014 |
| Total Posts: 14257 |
|
|
| 22 Jun 2015 05:25 AM |
that's kinky
brb updating all my scripts to use that |
|
|
| Report Abuse |
|
|
ShungTzu
|
  |
| Joined: 14 Jun 2014 |
| Total Posts: 959 |
|
|
| 22 Jun 2015 05:26 AM |
Also known as the Principle of Bivalence, thoroughly articulated about a hundred years ago, expounding on the law of the Excluded Middle, thoroughly articulated over 2 thousand years ago.
Too bad they don't teach logic in screwAll. |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2015 05:28 AM |
Teacher.teach(Logic.new("scewAll")) ._. |
|
|
| Report Abuse |
|
|
DrSaint
|
  |
| Joined: 14 Oct 2009 |
| Total Posts: 18429 |
|
|
| 22 Jun 2015 05:29 AM |
@Cody
That always trips me up when I see you use it.
I'll be reading the script normally, then see "KINKY" and i'm like
"Wait, what did I miss? Where's this coming from? Am I on the right forum? What kind of script is this??" |
|
|
| Report Abuse |
|
|
|
| 22 Jun 2015 05:35 AM |
| A true script... if you catch my drift ;) |
|
|
| Report Abuse |
|
|
DrSaint
|
  |
| Joined: 14 Oct 2009 |
| Total Posts: 18429 |
|
| |
|