Items and Monsters
How to give items, manage inventory, spawn monsters, and track damage.
Giving Items
Basic Item Give
ItemGive(aIndex, section, index, level)
ItemGive(aIndex, 0, 0, 9) -- Kris +9
ItemGive(aIndex, 14, 13, 0) -- Jewel of Bless
Advanced Item Give
For items with all options:
ItemGiveEx(aIndex, section, index, level, durability, skill, luck, option, exc, anc, socket1-5, socketBonus)
-- Perfect item +15 with all options
ItemGiveEx(
aIndex,
0, -- Section
0, -- Index
15, -- +15
255, -- Max durability
1, -- Has skill
1, -- Has luck
28, -- Options (4+4+4+16)
63, -- All excellent options
0, -- No ancient
0,0,0,0,0, -- No sockets
0 -- No socket bonus
)
Get Item Name
local itemId = (section * 512) + index
local itemName = ItemGetName(itemId)
Inventory Management
Inventory Sizes
local wearSize = InventoryGetWearSize() -- Usually 12
local mainSize = InventoryGetMainSize() -- Main inventory
local fullSize = InventoryGetFullSize() -- Total
Check Inventory
-- Get free slot count
local freeSlots = InventoryGetFreeSlotCount(aIndex)
if freeSlots < 1 then
NoticeSend(aIndex, 1, "Inventory full!")
return
end
-- Check if item fits
if InventoryCheckSpaceBySize(aIndex, 2, 4) ~= -1 then
ItemGive(aIndex, 0, 0, 0)
else
NoticeSend(aIndex, 1, "No space!")
end
-- Count specific item
local jewelCount = InventoryGetItemCount(aIndex, 14, 13)
LogPrint("Jewels of Bless: " .. jewelCount)
Reading Inventory
-- Get item at slot
local itemId = InventoryGetItemIndex(aIndex, 12)
if itemId ~= -1 then
local itemName = ItemGetName(itemId)
LogPrint("Item: " .. itemName)
end
-- Get full item data
local item = InventoryGetItemTable(aIndex, 12)
if item then
LogPrint("Level: " .. item.level)
LogPrint("Durability: " .. item.durability)
LogPrint("Excellent: " .. item.newOption)
end
Modifying Inventory
-- Modify item
local item = InventoryGetItemTable(aIndex, 12)
if item then
item.level = 15 -- Change to +15
item.newOption = 63 -- All excellent
InventorySetItemTable(aIndex, 12, item)
UserInfoSend(aIndex)
end
-- Remove item
InventoryDelItemIndex(aIndex, 12, 1) -- Remove 1 from slot 12
-- Remove specific item type
InventoryDelItemCount(aIndex, 14, 13, 5) -- Remove 5 Jewels of Bless
Monsters
Monster Indices
local minMonster = GetMinMonsterIndex() -- e.g., 1001
local maxMonster = GetMaxMonsterIndex() -- e.g., 10000
Creating Monsters
MonsterCreate(class, map, x, y, dir, aiType, aiGroup, targetIndex, npcTalkIndex)
local monsterIndex = MonsterCreate(
0, -- Class 0 (Bull Fighter)
0, -- Map (Lorencia)
130, -- X
130, -- Y
3, -- Direction (South)
0, -- Normal AI
0, -- No AI group
-1, -- No specific target
-1 -- No NPC dialog
)
if monsterIndex ~= -1 then
LogPrint("Monster spawned: " .. monsterIndex)
end
Deleting Monsters
MonsterDelete(monsterIndex)
Summons
-- Create summon (belongs to player)
local summonIndex = MonsterSummonCreate(aIndex, 50, 0, 130, 130, 3, 0)
-- Delete all player summons
MonsterSummonDelete(aIndex)
Monster Info
local monsterName = MonsterGetName(0) -- "Bull Fighter"
Damage Tracking
-- Get who damaged the monster
local damageTable = MonsterGetHitDamageTable(monsterIndex, 0)
if damageTable then
for i, entry in ipairs(damageTable) do
LogPrint(string.format("Player %d did %d damage",
entry.index, entry.damage))
end
end
Complete Example - Boss Event
BossEvent = {
bossIndex = -1,
}
function BossEvent:spawn()
self.bossIndex = MonsterCreate(
76, -- Golden Dragon
0, 130, 130, -- Lorencia center
3, 0, 0, -1, -1
)
if self.bossIndex ~= -1 then
NoticeSendToAll(0, "Boss spawned in Lorencia!")
end
end
-- When boss dies
BridgeFunction:push("OnMonsterDie", function(aIndex, bIndex)
if bIndex == BossEvent.bossIndex then
BossEvent:onBossKilled(bIndex)
end
end)
function BossEvent:onBossKilled(bIndex)
NoticeSendToAll(0, "Boss defeated!")
local damageTable = MonsterGetHitDamageTable(bIndex, 0)
if damageTable then
table.sort(damageTable, function(a, b)
return a.damage > b.damage
end)
-- Reward top 3
for i = 1, math.min(3, #damageTable) do
local entry = damageTable[i]
local playerIndex = entry.index
if GetObjectConnected(playerIndex) == OBJECT_ONLINE then
local zenReward = {10000000, 5000000, 2000000}
local itemLevel = {15, 13, 11}
-- Give zen
local zen = GetObjectMoney(playerIndex)
SetObjectMoney(playerIndex, zen + zenReward[i])
MoneySend(playerIndex, GetObjectMoney(playerIndex))
-- Give item
ItemGiveEx(playerIndex, 13, 8, itemLevel[i],
255, 1, 1, 28, 63, 0, 0,0,0,0,0,0)
NoticeSend(playerIndex, 0, string.format(
"Rank #%d Reward: %d Zen!",
i, zenReward[i]
))
end
end
end
self.bossIndex = -1
end
Best Practices
- Check inventory space before giving items
- Always validate player is online in async callbacks
- Update client after inventory changes
- Track monster indices for boss systems
- Sort damage tables for rankings
That's items and monsters! Next: database and events.