PicoPicoGameEngine

Quick Links

PicoPicoGameEngine's programming language is Lua5.2.

More detail see Lua web site.

The Programming Language Lua

Comment

-- Comment

--[[
Multi line
Comment.
]]

Variables

-- There are eight basic types in Lua: nil, boolean, number, string, function, userdata, thread, and table.
-- Lua is a dynamically typed language.

-- integer
a=10 

-- real number
b=10.5

-- strings
c="Hello"
d='Hello'

-- here document
e=[[abcd
efg
hij]]

print(e)

-- boolean
f=true
g=false

Arithmetic Operators

-- Operators
a=1+2 -- addition
b=4-3 -- subtraction
c=4*6 -- multiplication
d=8/2 -- division
e=5%3 -- modulo
f=4^2 -- exponentiation

Standard Output

print("test")

print "test"

Blocks

a is a global variable.
a="global a"

do
  
  -- b is a global variable.
  b="global b" 

  
  -- a is a local variable.
  local a="local a"

  print("inside do scope")

  print("a="..a) --a="local a"
  print("b="..b) --b="global b"

end

print("outside do scope")
print("a="..a) --a="global a"
print("b="..b) --b="global b"

Functions

-- Define function
function calc(a,b)

  -- return results
  -- you can return additional results use ','.
  return a+b,a*b

end

a,b=calc(10,4) -- call calc function.
print(a..","..b)

No name functions

-- Variables has a function.
calc=function(a,b)

  return a+b,a*b

end

a,b=calc(10,4) -- call calc function.
print(a..","..b)

if

a=4
if a==1 then
  print("a==1")
elseif a==2 then
  print("a==2")
elseif a==3 then
  print("a==3")
else
  print("other")
end

Other Operators

-- Relational operators
g=(a==b) 
h=(a>b) 
i=(a<b) 
j=(a>=b) 
k=(a<=b) 
l=(a~=b) 

-- Logical Operators
m=(g and h) 
n=(i or j) 
o=(not k) 

-- String concatenation operator
p="Hello ".."World"

for/while/repeat

-- for
for i=1,10,2 do

  print(i) 
end

-- while
i=1
while i<10 do
  print(i)
  i=i+1
end

-- repeat
i=1
repeat
  print(i)
  i=i+1
until i==10

-- break
for i=1,10 do
  if i>5 then
    break 
  end
  print(i)
end

Vararg parameters

function sum(...)

  
  local t={...}

  local s=0
  for i=1,#t do
    s=s+t[i]
  end
  return s

end

print(sum(1,2,3,4,5,6,7,8,9))

Coroutines

func=function()
  print("a")
  coroutine.yield() 
  print("b")
  coroutine.yield() 
  print("c")
end

-- create a coroutine
c=coroutine.wrap(func)

c() -- =>a
c() -- =>b
c() -- =>c
c() -- error

Create tables

t={}  

u={   
  a=1,
  b="abc",
  c=function(s)
    return s.a+2
  end
}

print("u.b="..u.b)

Change tables

t={} 


t.a=1
t.b="abc"


t["a"]=1
t["b"]="abc"

Array

t={}

t[1]=10 
t[2]=20
t[3]=30


print(#t) -- =>3

t[#t+1]=40 

u={10,20,30}

Iterators

t={
  a=10,
  b=20,
  c=30
}


for i,v in pairs(t) do
  print("t."..i.."="..v)
end


u={10,20,30}


for i,v in ipairs(u) do
  print("u["..i.."]="..v)
end

Instance method

t={
  a=3,
  b=5,
  add=function(s)
    return s.a+s.b -- 3+5  end
}

print(t:add())  -- (1)
print(t.add(t)) -- (2)

-- (1) and (2) are same.

String manipulation

-- There is string object for string manipulation.

t,u=string.byte("ABC",2,3)
print(t..","..u) --66,67


t=string.char(t,u)
print(t) --BC


a=12
t=string.format("a=%04d",a)
print(t) --a=0012


t,u=string.find("abcdef","cde")
print(t..","..u) --3,5


t=string.len("abcde")
print(t) --5


t=string.upper("abc")
print(t) --ABC


t=string.lower("ABC")
print(t) --abc


t=string.rep("Ab",3)
print(t) --AbAbAb


t=string.reverse("ABC")
print(t) --CBA


t=string.sub("ABCDEF",3)
print(t) --CDEF


t=string.sub("ABCDEF",3,5)
print(t) --CDE