0% found this document useful (0 votes)
7 views

lua basic

Uploaded by

fastkilar29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views

lua basic

Uploaded by

fastkilar29
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Here’s a comprehensive basic to advanced guide on Lua programming with explanations

and examples. You can copy and paste this into a PDF or document to follow along as you
learn.

Lua Programming Guide: Basic to Advanced

1. Introduction to Lua

What is Lua?

• Lua is a lightweight, high-performance scripting language often used in game


development, embedded systems, and applications like Roblox, Corona SDK, and
DaVinci Resolve Fusion.

Basic Syntax:

• Lua is case-sensitive.

• Statements are separated by a new line.

-- This is a comment

print("Hello, World!") -- Output: Hello, World!

2. Variables and Data Types

Variables

• Variables store data.

local name = "John" -- String

local age = 25 -- Number

local isAlive = true -- Boolean

local height = nil -- Nil (no value)

print(name, age, isAlive, height)

Data Types
1. String – Text data.

2. Number – Integer or floating-point values.

3. Boolean – true or false.

4. Nil – Represents no value.

5. Table – Key-value pair collection.

6. Function – Custom operations.

3. Operators

Arithmetic Operators

local a, b = 5, 2

print(a + b) -- Addition

print(a - b) -- Subtraction

print(a * b) -- Multiplication

print(a / b) -- Division

print(a % b) -- Modulus

print(a ^ b) -- Exponentiation

Relational Operators

print(5 > 2) -- true

print(5 == 2) -- false

print(5 ~= 2) -- true (not equal)

Logical Operators

print(true and false) -- false

print(true or false) -- true

print(not true) -- false

4. Control Structures
if-else Statement

local age = 18

if age >= 18 then

print("Adult")

else

print("Minor")

end

for Loop

for i = 1, 5 do

print("Value: " .. i)

end

while Loop

local count = 1

while count <= 3 do

print("Count: " .. count)

count = count + 1

end

repeat-until Loop

local i = 1

repeat

print("Repeat: " .. i)

i=i+1

until i > 3

5. Functions

Defining Functions
function greet(name)

return "Hello, " .. name

end

print(greet("Alice")) -- Output: Hello, Alice

Anonymous Functions

local add = function(a, b)

return a + b

end

print(add(3, 4)) -- Output: 7

6. Tables (Arrays and Dictionaries)

Arrays

local fruits = {"Apple", "Banana", "Cherry"}

for i = 1, #fruits do

print(fruits[i])

end

Dictionaries (Key-Value Pairs)

local person = {

name = "Alice",

age = 25

print(person.name) -- Output: Alice

Iterating Tables

for key, value in pairs(person) do

print(key .. ": " .. value)


end

7. Metatables and Object-Oriented Programming

Basic Metatable Example

local obj = {x = 10, y = 20}

local mt = {

__add = function(a, b)

return {x = a.x + b.x, y = a.y + b.y}

end

setmetatable(obj, mt)

local obj2 = {x = 5, y = 15}

setmetatable(obj2, mt)

local result = obj + obj2

print(result.x, result.y) -- Output: 15, 35

8. Error Handling

Using pcall

local status, err = pcall(function()

error("Something went wrong!")

end)

if not status then

print("Error: " .. err)


end

9. Advanced Concepts

Coroutines

Coroutines allow suspension and resumption of functions.

local co = coroutine.create(function()

for i = 1, 3 do

print("Step: " .. i)

coroutine.yield()

end

end)

coroutine.resume(co)

coroutine.resume(co)

Modules

You can modularize code into reusable files.

myModule.lua

local myModule = {}

function myModule.sayHello(name)

return "Hello, " .. name

end

return myModule

Main File

local myModule = require("myModule")


print(myModule.sayHello("John"))

10. Practical Examples

Game-Like Countdown Timer

local time = 10

while time > 0 do

print("Time left: " .. time)

time = time - 1

end

print("Game Over!")

11. Tips for Lua Development

1. Use tools like ZeroBrane Studio or VS Code with Lua plugins.

2. Learn debugging with print() and pcall().

3. Practice with Roblox Studio, Corona SDK, or Fusion’s scripting environment.

Key Takeaways

• Lua is lightweight and easy to learn for scripting tasks.

• Start with simple loops, functions, and tables.

• Move into advanced topics like metatables, coroutines, and modules.

• Combine practice with real-world examples (games, automation).

You might also like