Difference between revisions of "Basic lua: Types"

From The Official Visionaire Studio: Adventure Game Engine Wiki
 
(26 intermediate revisions by 2 users not shown)
Line 1: Line 1:
There are multiple lua types available, which are automatically determined by the returned value of the variable, function or table.
 
 
{| class="in"
 
{| class="in"
 
|-
 
|-
| class="i_arrow clickablecell" |  [[basic_lua:_Operators|Operators<]]
+
| class="i_arrow clickablecell" |  [[basic_lua:_Operators|<]]
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
| class="i_arrow clickablecell" | [[basic_lua:_Variables|>]]
+
| class="i_arrow clickablecell" | [[basic_lua:_Tables|>]]
 
|}
 
|}
== Conditional Operators ==
+
<hr>
 +
There are multiple lua types available, which are automatically determined by the returned value of the variable, function or table.
 +
 
 +
== Types ==
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
| boolean || width="90%" | This returns a value of '''true''' or '''false'''.
+
| <span class="vsyellow">boolean</span> || width="90%" | This returns a value of '''true''' or '''false'''.
 
|-
 
|-
| function || A function usually contains a list of actions to be executed & can often be used to calculate & return values.
+
| <span class="vsyellow">function</span> || A function usually contains a list of actions to be executed & can often be used to calculate & return values.
 
|-
 
|-
| nil || If no data type is returned then whatever you queried is either empty or does not exist, thus it is '''nil'''.
+
| <span class="vsyellow">nil</span> || If no data type is returned then whatever you queried is either empty or does not exist, thus it is '''nil'''.
 
|-
 
|-
| number || This usually contains an integer (whole) or floating point (decimal) number.
+
| <span class="vsyellow">number</span> || This usually contains an integer (whole) or floating point (decimal) number.
 
|-
 
|-
| string || This usually contains text or numbers wrapped in "quotation marks" or 'apostrophes'.
+
| <span class="vsyellow">string</span> || This usually contains text or numbers wrapped in "quotation marks" or 'apostrophes'.
 
|-
 
|-
| table || This usually consists of an array, or multiple arrays of data & data types.
+
| <span class="vsyellow">table</span> || This usually consists of an array, or multiple arrays of data & data types.
|-
 
| threads || -
 
|-
 
| userdata || -
 
 
|}  
 
|}  
  
 +
=== Boolean ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 1: boolean !!
+
! prints true !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = true
 
local a = true
 +
 
print( a, type(a) )
 
print( a, type(a) )
</syntaxhighlight> || width="200px" | [[File:lb_types_001.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_1.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== Function ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 2: function !!
+
! prints 7 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
function a(i)
 
function a(i)
 
  return i
 
  return i
Line 46: Line 47:
  
 
print( a(7), type(a) )
 
print( a(7), type(a) )
</syntaxhighlight> || width="200px" | [[File:lb_types_002.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_2.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== Nil ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 3: if condition is false !!
+
! prints nil, nil !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
local a = false
+
local a = nil
  
if a == false then
+
print( a, "  type = " .. type(a) )
print("a = false")
+
print( b, " type = " .. type(b) )
end
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_3.png|frameless|center|180px]]
</syntaxhighlight> || width="200px" | [[File:lb_operators_004.png|thumb|right|180px|click to enlarge]]
 
 
|}
 
|}
  
 +
=== Number ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 4: if condition is not met, then do else !!
+
! prints 7, 7.25 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
local a = false
+
local a = 7
 +
local b = 7.25
  
if a then
+
print( a, type(a) )
print("a = true")
+
print( b, type(b) )
else
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_4.png|frameless|center|180px]]
print("a = false")
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_005.png|thumb|right|180px|click to enlarge]]
 
 
|}
 
|}
  
 +
=== String ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 5: if condition is not met, then do elseif !!
+
! prints hello world !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
local a = false
+
local a = "hello world"
  
if a then
+
print( a, type(a) )
print("a = true")
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_5.png|frameless|center|180px]]
elseif not a then
 
print("a = false")
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_006.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
== Logical Operators ==
 
{| class="ts"
 
|-
 
| and || width="90%" | Allows you to check multiple conditions in a single if query.
 
|-
 
| or || Allows you to add an alternative if query into a single if query.
 
|-
 
| not || Checks if condition equals false or negative.
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 1: if condition a is true and condition b is false then !!
 
|-
 
| <syntaxhighlight>
 
local a = true
 
local b = false
 
 
 
if a == true and b == false then
 
print("a = true", "b=false")
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_007.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 2: if condition is true or condition == true then !!
 
|-
 
| <syntaxhighlight>
 
local a = true
 
 
 
if a or a == true then
 
print("a = true")
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_008.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 3: if condition is not true !!
 
|-
 
| <syntaxhighlight>
 
local a = false
 
 
 
if not a then
 
print("a = false")
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_003.png|thumb|right|180px|click to enlarge]]
 
 
|}
 
|}
  
== Comparison Operators ==
+
=== Table ===
{| class="ts"
 
|-
 
| <big>==</big> || width="90%" | Equal to.
 
|-
 
| <big>~=</big> || Does not equal.
 
|-
 
| <big>>=</big> || Greater than or equal to.
 
|-
 
| <big>></big> || Greater than.
 
|-
 
| <big><=</big> || Less than or equal to.
 
|-
 
| <big><</big> || Less than.
 
|}
 
 
 
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 1: if a equals 1 !!
+
! prints 1, 2, 3, 4, 5, 6, 7, 8 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
local a = 1
+
local a = {1,1+1,1*3,2*2,20/4,"6",3.5*2,10-2}
  
if a == 1 then
+
print( a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], type(a) )
print("a = 1")
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_6.png|frameless|center|180px]]
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_009.png|thumb|right|180px|click to enlarge]]
 
 
|}
 
|}
 
+
<hr>
{| class="ts"
 
|-
 
! Example 2: if a does not equal 2 !!
 
|-
 
| <syntaxhighlight>
 
local a = 1
 
 
 
if a ~= 2 then
 
print("a = " .. a)
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_010.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 3: if a is greater than or equal to 2 !!
 
|-
 
| <syntaxhighlight>
 
local a = 5
 
 
 
if a >= 2 then
 
print("a = " .. a)
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_011.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 4: if a is greater than 2 !!
 
|-
 
| <syntaxhighlight>
 
local a = 3
 
 
 
if a > 2 then
 
print("a = " .. a)
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_012.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 5: if a less than or equal to 2  !!
 
|-
 
| <syntaxhighlight>
 
local a = 2
 
 
 
if a <=  2 then
 
print("a = " .. a)
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_013.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 6: if a less than 2  !!
 
|-
 
| <syntaxhighlight>
 
local a = 0
 
 
 
if a <  2 then
 
print("a = " .. a)
 
end
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_014.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
== Mathematical Operators ==
 
{| class="ts"
 
|-
 
| <big>+</big> || width="90%" | Add
 
|-
 
| <big>-</big> || Subtract or invert
 
|-
 
| <big>*</big> || Multiply
 
|-
 
| <big>/</big> || Divide
 
|-
 
| <big>^</big> || Power
 
|-
 
| <big>%</big> || Remainder
 
|}
 
 
 
{| class="ts"
 
|-
 
!  Example 1: 1 +2 = 3 !!
 
|-
 
| <syntaxhighlight>
 
local a = 1
 
local b = 2
 
local result = a + b
 
 
 
print( result )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_015.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 2: 3 - 2 = 1 !!
 
|-
 
| <syntaxhighlight>
 
local a = 3
 
local b = 2
 
local result = a - b
 
 
 
print( result )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_016.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 3: inversed values !!
 
|-
 
| <syntaxhighlight>
 
local a = 3
 
local result = -a
 
 
 
print( "original value: " .. a, "inversed value: " .. result )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_017.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 4: 2 x 2 = 4 !!
 
|-
 
| <syntaxhighlight>
 
local a = 2
 
local result = a * a
 
 
 
print( result )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_018.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 5: 10 ÷ 5 = 2  !!
 
|-
 
| <syntaxhighlight>
 
local a = 10
 
local b = 5
 
local result = a / b
 
 
 
print( result )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_019.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
! Example 6: base of 2, power of 8 (2ⁿ) = 256  !!
 
|-
 
| <syntaxhighlight>
 
local a = 2
 
local b = 8
 
local result = 2 ^ 8
 
 
 
-- break down power function into string result (ignore this code)
 
function power(i1, i2)
 
local val = ""
 
--+--
 
for i = 1, i2 do
 
  if i < i2 then val = val..i1.."*" else val = val..i1 end
 
end
 
return "which is the equivalent of " .. val
 
end
 
 
 
print("power value of 2 & 8 = " .. result, power(a,b))
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_020.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
{| class="ts"
 
|-
 
!  Example 7: remainder of 25 ÷ 4 = 1 !!
 
|-
 
| style="max-width: 700px;" | <syntaxhighlight>
 
local a = 25
 
local b = 4
 
local result = a / 4
 
local remainder = a % b
 
local decimal = remainder / b
 
 
 
print("result: 25 ÷ 4 = "  .. result, "remainder: .25 x 4 = " .. remainder, "decimal: 1 ÷ 4 = " .. decimal )
 
</syntaxhighlight> || width="200px" | [[File:lb_operators_021.png|thumb|right|180px|click to enlarge]]
 
|}
 
 
 
 
{| class="in"
 
{| class="in"
 
|-
 
|-
| class="i_arrow clickablecell" |  [[basic_lua:_Operators|Operators<]]
+
| class="i_arrow clickablecell" |  [[basic_lua:_Operators|<]]
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
 
| class="i_norm clickablecell" | '''[[basic_lua:_Index|Index]]'''
| class="i_arrow clickablecell" | [[basic_lua:_Variables|>]]
+
| class="i_arrow clickablecell" | [[basic_lua:_Tables|>]]
|}
+
|}{{toc}}
{{toc}}
 

Latest revision as of 03:16, 3 September 2022

< Index >

There are multiple lua types available, which are automatically determined by the returned value of the variable, function or table.

Types

boolean This returns a value of true or false.
function A function usually contains a list of actions to be executed & can often be used to calculate & return values.
nil If no data type is returned then whatever you queried is either empty or does not exist, thus it is nil.
number This usually contains an integer (whole) or floating point (decimal) number.
string This usually contains text or numbers wrapped in "quotation marks" or 'apostrophes'.
table This usually consists of an array, or multiple arrays of data & data types.

Boolean

prints true
local a = true

print( a, type(a) )
Basic lua (types) 1.png

Function

prints 7
function a(i)
 return i
end

print( a(7), type(a) )
Basic lua (types) 2.png

Nil

prints nil, nil
local a = nil

print( a, "  type = " .. type(a) )
print( b, "  type = " .. type(b) )
Basic lua (types) 3.png

Number

prints 7, 7.25
local a = 7
local b = 7.25

print( a, type(a) )
print( b, type(b) )
Basic lua (types) 4.png

String

prints hello world
local a = "hello world"

print( a, type(a) )
Basic lua (types) 5.png

Table

prints 1, 2, 3, 4, 5, 6, 7, 8
local a = {1,1+1,1*3,2*2,20/4,"6",3.5*2,10-2}

print( a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], type(a) )
Basic lua (types) 6.png

< Index >