Skip to content

MobPath

A path a mob has searched with MobEntity:findPath: the way it would walk to a target, from node to node. A spell can look at it before the mob takes a step, for example to compare the ways to two targets, or to see whether a way leads through a place the mob should avoid, and then let the mob walk along it with MobEntity:startPathAlong.

A path tells how the world was when it was searched. It doesn't change when blocks do, and it can't be changed by a spell. To let a mob walk another way, give MobEntity:findPath a list of positions to come by.

Example

local pig = spell:summon("pig") --[[@as MobEntity]]
local path = pig:findPath(spell.owner.pos)
if path and path.reachable then
  print(string.format("%.1f blocks", path.length))
  for _, node in ipairs(path.nodes) do
    spell:execute(string.format("/particle minecraft:end_rod %s %s %s", node.x, node.y + 0.5, node.z))
  end
end

Fields

length: number — Read-Only

The length of the path in blocks, from node to node.

local pig = spell:summon("pig") --[[@as MobEntity]]
local path = pig:findPath(spell.pos + Vec3(10, 0, 0))
print(path and path.length .. " blocks to walk")

nodes: Vec3[ ] — Read-Only

Copies of the positions the path leads through, in order: the middle of each block, at the height of that block's bottom. The first is where the mob was when the path was searched, the last is the end of the path.

local pig = spell:summon("pig") --[[@as MobEntity]]
local path = pig:findPath(spell.pos + Vec3(10, 0, 0))
for _, node in ipairs(path and path.nodes or {}) do
  spell.pos = node
  spell.block = Block("torch") -- marks the way
end

reachable: boolean — Read-Only

false when the path stops short of the target: the target can't be reached, or it is further away than the search looked.

local pig = spell:summon("pig") --[[@as MobEntity]]
local path = pig:findPath(spell.pos + Vec3(10, 0, 0))
if path and not path.reachable then
  print("The pig can't get there")
end

target: Vec3 — Read-Only

A copy of the position the path was searched to: the last one, if it was searched by way of others.

local pig = spell:summon("pig") --[[@as MobEntity]]
local path = pig:findPath(spell.pos + Vec3(10, 0, 0))
print(path and path.target)