Learning
레슨 1 / 3·15분

Lua 기본 문법

Lua 시작하기

Lua는 가볍고 빠른 스크립팅 언어로, 게임 개발(Roblox, WoW 등)과 임베디드 시스템에서 널리 사용됩니다. local 키워드로 지역 변수를 선언하고, print()로 출력합니다.

lua
-- 변수 선언
local name = "Lua"
local version = 5.4
local is_fast = true
local nothing = nil  -- 값 없음

print("Hello, " .. name .. "!")  -- 문자열 결합은 ..
print("버전: " .. tostring(version))

조건문과 반복문

Lua의 제어문은 if ... then ... end, for ... do ... end 형태입니다. 블록을 end로 닫는 것이 특징입니다.

lua
-- if / elseif / else
local score = 85

if score >= 90 then
    print("A")
elseif score >= 80 then
    print("B")
else
    print("C")
end

-- 숫자 for 반복
for i = 1, 5 do
    io.write(i .. " ")  -- 1 2 3 4 5
end
print()

-- while 반복
local count = 0
while count < 3 do
    print("count: " .. count)
    count = count + 1
end
  • local — 지역 변수 선언 (없으면 전역)
  • print() — 줄바꿈 포함 출력
  • .. — 문자열 결합 연산자
  • nil — 값 없음 (다른 언어의 null)
  • -- — 한 줄 주석, --[[ ... ]] — 여러 줄 주석
💡

Lua에서 local 없이 선언한 변수는 전역(global)이 됩니다. 예기치 않은 충돌을 방지하려면 항상 local을 붙이세요.