Skip to content

Mob AI

Define and assign custom AI behaviours to mobs. Any MobEntity subtype returned by world:spawn() is a MobWrapper with AI methods.

Metatable name: "mob"

mc.registerBehaviour("my_behaviour", function(self, state)
-- self: the mob wrapper
-- state: persistent state table (initially empty), shared across ticks
if self.target then
self:navigateTo(self.target)
end
end)
-- By registered ID
mob:setAI("my_behaviour")
-- By inline function
mob:setAI(function(self, state)
self:lookAt(self.target)
self:moveToward(self.target.pos, self.speed)
end)
-- Clear AI
mob:clearAI()
PropertyTypeDescription
mob.isMobbooleanAlways true
mob.targetentity or nilCurrent target entity. Read/write — assign an entity wrapper (player, mob, etc.), or nil to clear. Only LivingEntity types are valid targets; non-living entities are silently ignored.
mob.speednumberMovement speed (entity attribute)
mob.agenumberMob age in ticks. Read-only.
mob.pathRemainingnumberPath completion progress (0–1)
mob.pathFoundbooleanWhether a valid path was found
mob.aiActivebooleanWhether a behaviour is currently running
MethodDescription
mob:navigateTo(x, y, z, speed?)Navigate to a position (returns true if path started)
mob:navigateTo(entity, speed?)Navigate to an entity (returns true if path started)
mob:stopNavigation()Stop current pathfinding
mob:lookAt(x, y, z)Look at a position
mob:lookAt(entity)Look at an entity
mob:moveToward(vec, speed)Move toward a vector at given speed (speed defaults to 1.0)
mob:jump()Make the mob jump
mob:tryAttack(entity)Attempt to attack an entity. Returns true on success.
mob:canSee(entity)Returns true if the mob has line of sight
mob:distanceTo(entity)Returns distance to entity or vector
mob:distanceTo(vec)Returns distance to entity or vector
register("pet", function(ctx)
local p = ctx.player
local spawnAt = p.pos + vec(2, 0, 0)
local mob = p.world:spawn("wolf", spawnAt)
mob:setAI("pet")
end)
BehaviourDescription
guardFollows and protects the nearest player, attacks hostile mobs
petFollows the owner, teleports when too far
orbiterCircles around a fixed point
statueStands still, faces its spawn direction
wanderRandom wandering within a radius
mob:setAI("orbiter")

world:spawn() returns a MobWrapper for mob types:

local zombie = world:spawn("zombie", vec(100, 64, 200), {
on_spawn = function(mob)
mob:setAI("guard")
mob.speed = 0.3
end
})