## 프로젝트 개요
게임에서 사용되는 아이템을 효율적으로 관리하고, 유저에게 명확한 정보를 제공하는 시스템입니다. 콘솔 기반으로 실행되며, 데이터를 파일에 저장하여 재시작 시에도 상태를 유지합니다.
## 주요 기능
- 아이템 생성 및 목록 조회
- 아이템 삭제 및 수정
- 파일로 데이터 저장 및 불러오기
- 사용자 입력 처리 및 에러 검증
## 사용 방법
1. 스크립트를 실행합니다.
2. 명령어에 따라 아이템을 관리합니다 (예: add, list, delete 등)
3. 파일이 생성되어 데이터가 저장됩니다.
## 확장 가능성
- 그래픽 인터페이스 추가
- 아이템 종류별 분류
- 사용자 계정 기반의 아이템 관리
🌙 게임 내 아이템 관리 시스템 구현
Lua 예제
중급
난이도
예제
타입
11/07
등록일
게임 내 아이템 관리 시스템 구현
중급태그
코드 예제
-- 게임 내 아이템 관리 시스템
-- 파일 저장 경로
local savefile = "items.save"
-- 아이템 데이터 구조
table.items = {
{id=1, name="철 검", type="무기", rarity="보통"},
{id=2, name="마법 책", type="소품", rarity="희귀"}
}
-- 아이템 ID를 기준으로 찾는 함수
local function findItemById(id)
for i, item in ipairs(table.items) do
if item.id == id then
return item
end
end
return nil
end
-- 파일에서 데이터 불러오기
local function loadItems()
local file = io.open(savefile, "r")
if not file then
print("저장된 아이템 데이터가 없습니다.")
return
end
local content = file:read("*a")
file:close()
-- JSON 형식으로 저장된 데이터를 불러오기 (간단한 구조)
local data = {}
for line in content:gmatch("([^
]+)
") do
table.insert(data, line)
end
if #data > 0 then
table.items = {}
for _, itemStr in ipairs(data) do
local id, name, type, rarity = itemStr:match("(%d+), (%w+), (%w+), (%w+)")
table.insert(table.items, {id=id, name=name, type=type, rarity=rarity})
end
end
end
-- 데이터 저장 함수
local function saveItems()
local file = io.open(savefile, "w")
if not file then
print("데이터를 저장할 수 없습니다.")
return
end
for _, item in ipairs(table.items) do
file:write(string.format("%d, %s, %s, %s
", item.id, item.name, item.type, item.rarity))
end
file:close()
end
-- 사용자 입력 처리
local function processCommand(cmd)
local args = {}
for arg in cmd:gmatch("(%S+) ") do
table.insert(args, arg)
end
if #args == 0 then return end
local command = args[1]
if command == "add" then
-- 아이템 추가
if #args < 2 then
print("사용법: add [이름] [타입] [난도]")
return
end
local name = args[2]
local type = args[3]
local rarity = args[4]
local newItem = {
id=#table.items + 1,
name=name,
type=type,
rarity=rarity
}
table.insert(table.items, newItem)
print(string.format("아이템 "%s" 추가됨 (ID: %d)", name, newItem.id))
elseif command == "list" then
-- 아이템 목록 보기
if #table.items == 0 then
print("등록된 아이템이 없습니다.")
return
end
print("=== 아이템 목록 ===")
for _, item in ipairs(table.items) do
print(string.format("ID: %d | 이름: %s | 타입: %s | 난도: %s", item.id, item.name, item.type, item.rarity))
end
print("=== 끝 ===")
elseif command == "delete" then
-- 아이템 삭제
if #args < 2 then
print("사용법: delete [ID]")
return
end
local id = tonumber(args[2])
local item = findItemById(id)
if not item then
print(string.format("ID %d의 아이템을 찾을 수 없습니다.", id))
return
end
table.remove(table.items, findItemById(id))
print(string.format("아이템 ID %d 삭제됨", id))
elseif command == "edit" then
-- 아이템 수정
if #args < 2 then
print("사용법: edit [ID] [필드] [값]")
return
end
local id = tonumber(args[2])
local field = args[3]
local value = args[4]
local item = findItemById(id)
if not item then
print(string.format("ID %d의 아이템을 찾을 수 없습니다.", id))
return
end
item[field] = value
print(string.format("아이템 ID %d 수정됨 (필드: %s -> %s)", id, field, value))
else
-- 알 수 없는 명령어 처리
print(string.format("명령어 "%s"는 존재하지 않습니다.", command))
end
end
-- 메인 루프
while true do
print("
=== 아이템 관리 시스템 ===")
print("명령어: add, list, delete, edit, exit")
local cmd = io.read()
if cmd == "exit" then break end
processCommand(cmd)
saveItems()
end
등록일: 2025년 11월 07일 02:35
언어 정보
언어
Lua
카테고리
General
인기도
#15
학습 팁
코드를 직접 실행해보세요
변수를 바꿔가며 실험해보세요
오류가 나도 포기하지 마세요
다른 예제도 찾아보세요