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, ctx)
-- self: the mob wrapper
-- ctx: {deltaTime, tick}
if self.target then
self:navigateTo(self.target)
end
end)
-- By registered ID
mob:setAI("my_behaviour")
-- By inline function
mob:setAI(function(self, ctx)
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
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)Navigate to a position
mob:navigateTo(entity)Navigate to an entity
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
mob:jump()Make the mob jump
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
})