Skip to content

Monster

Monster Class

The Monster class represents monster creatures in the game. It inherits from Creature.

Constructor

Monster(id)
Monster(name)
ParameterTypeDescription
idnumberMonster creature ID
namestringMonster name

Type Checking

isMonster()

Returns true if the creature is a monster.

if creature:isMonster() then
local monster = creature:getMonster()
end

Basic Properties

MethodReturnsDescription
getId()numberCreature ID
getName()stringMonster name
getType()MonsterTypeMonster type data

Spawn & Respawn

MethodReturnsDescription
getSpawnPosition()PositionOriginal spawn position
isInSpawnRange(position)booleanIs within spawn range
setSpawnPosition(position)-Set spawn position
local spawnPos = monster:getSpawnPosition()
if not monster:isInSpawnRange(monster:getPosition()) then
monster:teleportTo(spawnPos)
end

Target & Combat

MethodReturnsDescription
getTarget()CreatureCurrent target
setTarget(creature)-Set attack target
selectTarget(creature)booleanSelect new target
searchTarget(searchType)booleanSearch for target
isTarget(creature)booleanIs creature a target
isOpponent(creature)booleanIs creature an opponent
isFriend(creature)booleanIs creature a friend

Search Types:

  • TARGETSEARCH_DEFAULT (0)
  • TARGETSEARCH_RANDOM (1)
  • TARGETSEARCH_ATTACKRANGE (2)
  • TARGETSEARCH_NEAREST (3)
-- Make monster target player
monster:selectTarget(player)
-- Search for nearest target
monster:searchTarget(TARGETSEARCH_NEAREST)

Master & Summons

MethodReturnsDescription
getMaster()CreatureMaster (if summoned)
setMaster(creature)booleanSet master
isSummon()booleanIs a summon
if monster:isSummon() then
local master = monster:getMaster()
if master and master:isPlayer() then
print("Summoned by: " .. master:getName())
end
end

Behavior

MethodReturnsDescription
isIdle()booleanIs idle
setIdle(idle)-Set idle state
isWalkingToSpawn()booleanWalking to spawn
isIgnoringFieldDamage()booleanIgnoring field damage

Loot & Corpse

MethodReturnsDescription
setDropLoot(drop)-Enable/disable loot
setSkillLoss(loss)-Enable/disable skill loss
-- Disable loot for arena monster
monster:setDropLoot(false)

Examples

Spawn Monster

local function spawnMonster(name, position)
local monster = Game.createMonster(name, position, true, true)
if monster then
monster:getPosition():sendMagicEffect(CONST_ME_TELEPORT)
return monster
end
return nil
end

Create Summon

local function createSummon(player, monsterName)
local position = player:getPosition()
position:getNextPosition(player:getDirection())
local summon = Game.createMonster(monsterName, position, true, true)
if summon then
summon:setMaster(player)
return summon
end
return nil
end

Monster Death Handler

local deathEvent = CreatureEvent("MonsterDeath")
function deathEvent.onDeath(creature, corpse, killer, mostDamageKiller, lastHitUnjustified, mostDamageUnjustified)
if not creature:isMonster() then
return true
end
local monster = creature:getMonster()
local monsterType = monster:getType()
if killer and killer:isPlayer() then
local player = killer:getPlayer()
player:sendTextMessage(MESSAGE_INFO_DESCR,
"You killed a " .. monster:getName() .. "!")
end
return true
end
deathEvent:register()

Check Monster Type

local function isBoss(monster)
local monsterType = monster:getType()
return monsterType:isBoss()
end
local function getMonsterExperience(monster)
local monsterType = monster:getType()
return monsterType:getExperience()
end