Toggle menu
Toggle personal menu
Not logged in
Your IP address will be publicly visible if you make any edits.

Tutorials/Types/Booleans: Difference between revisions

From FiguraMC
Manuel (talk | contribs)
Automated upload of converted .txt file.
 
m why do y'all write so big
 
Line 1: Line 1:
A boolean is a Lua value that is either <code>true</code> or <code>false</code>.
A boolean is a Lua value that is either <code>true</code> or <code>false</code>. Additionally, Lua considers ''any value'' except <code>nil</code> and <code>false</code> a truthy value. A truthy value in the condition part (<code>if CONDITION then</code>) part of a condition will act like true and run the code in the <code>then</code> section.
 
 
 
Additionally, Lua treats all values as if they’re ‘truthy’ or ‘falsey’.
 
 
 
If a value is truthy and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s true.
 
 


Example:
Example:


<syntaxhighlight lang="lua">if 0 then
<syntaxhighlight lang="lua">if 0 then
 
     -- numbers are truthy, so this will always be printed
     log("truthy") -- numbers are truthy so this will always be logged
     print("truthy")
 
else
 
     log("falsey")
 
end</syntaxhighlight>
end</syntaxhighlight>


If a value is falsey and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s false.
If a value is falsey and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s false. This will skip the code after <code>if</code>, and any <code>else</code> code will be run, if present.
 
 


Example:
Example:


<syntaxhighlight lang="lua">if nil then
<syntaxhighlight lang="lua">if nil then
 
    -- this will never be reached
     log("truthy")
     print("truthy")
 
else
else
 
     -- nil is falsey, so this will always be printed
     log("falsey") -- nil is falsey so this will always be logged
    print("falsey")
 
end</syntaxhighlight>
end</syntaxhighlight>


The only values that are falsey are false and nil, every other value (numbers, tables, modelparts, etc) is truthy. (nil as a value means there’s no information. It’s literally nothing)
The only values that are falsey are false and nil, every other value (numbers, tables, modelparts, etc) is truthy. (nil as a value means there’s no information. It’s literally nothing)


 
The <code>not</code> operator flips the truthiness of the value into true or false. For example, `not true`, `not 0`, and `not hello` are all false, while `not false` and `not nil` are true.
 
The <code>not</code> operator flips the truthiness of the value into true or false.
 
 


Examples (all of these are true statements):
Examples (all of these are true statements):


<syntaxhighlight lang="lua">not true == false
<syntaxhighlight lang="lua">not true == false
not false == true
not false == true
not nil == true -- a non-boolean value is turned into a boolean
not nil == true -- a non-boolean value is turned into a boolean
not models == false -- models is a modelpart and truthy, so flipping it turns it into false</syntaxhighlight>
not models == false -- models is a modelpart and truthy, so flipping it turns it into false</syntaxhighlight>


When used in methods <code>true</code> usually activates something, and <code>false</code> deactivates it. However in some places returning true may turn something off, always read the description of a method, field, or event to discover what boolean does what.
When used in methods <code>true</code> usually activates something, and <code>false</code> deactivates it. However in some places returning true may turn something off, always read the description of a method, field, or event to discover what boolean does what.

Latest revision as of 04:29, 27 September 2024

A boolean is a Lua value that is either true or false. Additionally, Lua considers any value except nil and false a truthy value. A truthy value in the condition part (if CONDITION then) part of a condition will act like true and run the code in the then section.

Example:

if 0 then
    -- numbers are truthy, so this will always be printed
    print("truthy")
end

If a value is falsey and it’s in an if statement or a function that wants a boolean value, it’s treated like it’s false. This will skip the code after if, and any else code will be run, if present.

Example:

if nil then
    -- this will never be reached
    print("truthy")
else
    -- nil is falsey, so this will always be printed
    print("falsey")
end

The only values that are falsey are false and nil, every other value (numbers, tables, modelparts, etc) is truthy. (nil as a value means there’s no information. It’s literally nothing)

The not operator flips the truthiness of the value into true or false. For example, `not true`, `not 0`, and `not hello` are all false, while `not false` and `not nil` are true.

Examples (all of these are true statements):

not true == false
not false == true
not nil == true -- a non-boolean value is turned into a boolean
not models == false -- models is a modelpart and truthy, so flipping it turns it into false

When used in methods true usually activates something, and false deactivates it. However in some places returning true may turn something off, always read the description of a method, field, or event to discover what boolean does what.