Difference between revisions of "Basic lua: Types"

From The Official Visionaire Studio: Adventure Game Engine Wiki
 
(15 intermediate revisions by the same user 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"
 
|-
 
|-
Line 6: Line 5:
 
| class="i_arrow clickablecell" | [[basic_lua:_Tables|>]]
 
| class="i_arrow clickablecell" | [[basic_lua:_Tables|>]]
 
|}
 
|}
 +
<hr>
 +
There are multiple lua types available, which are automatically determined by the returned value of the variable, function or table.
 +
 
== Types ==
 
== 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.
 
|}  
 
|}  
  
 +
=== 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 43: 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: nil !!
+
! prints nil, nil !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = nil
 
local a = nil
  
 
print( a, "  type = " .. type(a) )
 
print( a, "  type = " .. type(a) )
 
print( b, "  type = " .. type(b) )
 
print( b, "  type = " .. type(b) )
</syntaxhighlight> || width="200px" | [[File:lb_types_003.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_3.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== Number ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 4: number !!
+
! prints 7, 7.25 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = 7
 
local a = 7
 
local b = 7.25
 
local b = 7.25
Line 68: Line 74:
 
print( a, type(a) )
 
print( a, type(a) )
 
print( b, type(b) )
 
print( b, type(b) )
</syntaxhighlight> || width="200px" | [[File:lb_types_004.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_4.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== String ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 5: string !!
+
! prints hello world !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = "hello world"
 
local a = "hello world"
  
 
print( a, type(a) )
 
print( a, type(a) )
</syntaxhighlight> || width="200px" | [[File:lb_types_005.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_5.png|frameless|center|180px]]
 
|}
 
|}
  
 +
=== Table ===
 
{| class="ts"
 
{| class="ts"
 
|-
 
|-
! Example 6: table !!
+
! prints 1, 2, 3, 4, 5, 6, 7, 8 !!
 
|-
 
|-
| <syntaxhighlight>
+
| <syntaxhighlight lang="lua">
 
local a = {1,1+1,1*3,2*2,20/4,"6",3.5*2,10-2}
 
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) )
 
print( a[1], a[2], a[3], a[4], a[5], a[6], a[7], a[8], type(a) )
</syntaxhighlight> || width="200px" | [[File:lb_types_006.png|thumb|right|180px|click to enlarge]]
+
</syntaxhighlight> || width="180px" style="vertical-align:middle;" | [[File:Basic_lua_(types)_6.png|frameless|center|180px]]
 
|}
 
|}
 
+
<hr>
 
{| class="in"
 
{| class="in"
 
|-
 
|-

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 >