🌐

Introduction to Lua Programming Language

Aug 22, 2024

Lua Course Lecture Notes

Introduction to Lua

  • Lua is a procedural programming language.
  • It is used to develop software, work with machine learning, and create games (from text games to VR games).
  • Popular for game development; often paired with C/C++.

Course Overview

  • Difficulty: Relatively easy; no prior programming knowledge required.
  • Duration: About 4-5 hours; can be completed in about a week with determination.
  • Execution Speed: Fast; suitable for games.
  • Types: Procedural and object-oriented.

Projects Developed with Lua

  • Adobe Photoshop Lightroom
  • Apache HTTP Server
  • Roblox games
  • Angry Birds
  • World of Warcraft

Getting Started

Installation of Lua

  • To install Lua on Linux (Arch): sudo pacman -S lua
  • For Windows/Mac: Download from the Lua website.

Creating Your First Lua File

  • Ensure the file ends with .lua, not .txt (Windows users).
  • Use a code editor (e.g., VS Code) to write code.

Printing Text

  • Use print() function to display text.
  • Example: print("Hello World")

Comments

  • Single line comment: -- Comment
  • Multi-line comment: --[[ Comment ]]

Basic Types in Lua

  • Nil: Represents no value.
  • Number: e.g., 1, 0.1, 44.5.
  • String: Text enclosed in quotes.
  • Boolean: True/False.
  • Tables: Complex data structures (arrays, dictionaries).

Variables

  • Create a variable using local keyword.
  • Example: local a = 10
  • Variables can change values over time.

Basic I/O

  • Use terminal for I/O operations.

Mathematical Operations

  • Basic arithmetic: +, -, *, /.
  • Order of operations: parentheses, exponents, multiplication/division, addition/subtraction.
  • Use math library for more complex operations.*

Strings in Lua

  • Strings can be concatenated with .. operator.
  • Escape characters: e.g., backslash \
  • String length: #string

Control Structures

If Statements

  • Syntax: if condition then ... end
  • Use comparison operators (>, <, ==).

Loops

  • For Loop: for i = 1, 10 do ... end
  • While Loop: while condition do ... end

Functions

  • Declare functions with function keyword.
  • Can return values using return keyword.
  • Functions can have parameters.

OOP in Lua

  • Simulate OOP with tables and functions.
  • Create objects using functions returning tables.
  • Inheritance can be simulated with metatables.

Metatables

  • Use metatables to define custom behaviors for tables (e.g., addition).
  • Define meta methods like __add, __sub, etc.

Modules in Lua

  • Define reusable code in separate files (modules).
  • Use require to import a module.

OS Module

  • Interact with the operating system.
  • Get environment variables, execute commands, and manipulate files.

Conclusion

  • Lua is versatile and great for game development.
  • Explore libraries and further resources for learning Lua.