Skip to content

Orders and commands ​

Units act only through orders: move, attack, cast, gather, or one of your own. Players change the world only through commands, which are plain data checked at the start of the next tick. Giving a unit an order is one kind of command.

Quick start ​

ts
knight.order('move', { x: 24, z: 24 })
knight.order('attack', enemy, { queue: true }) // after what it was told, as Shift does
knight.order('cast', { ability: 'holyLight', target: footman.id })

// for many units, or for a player who isn't the owner
world.issue({ type: 'order', player: 1, entities: [knight.id], order: { type: 'move', point: { x: 24, z: 24 } } })

Common tasks ​

Write an order ​

ts
import { moveTo, moving, type OrderBehavior, type Vec2 } from '@v12-engine/engine'

const ROAD: Vec2[] = [
	{ x: 6, z: 6 },
	{ x: 30, z: 6 },
	{ x: 30, z: 30 }
]

/** Walks the road, one point after the other, and is done at the end of it */
export const walkRoad: OrderBehavior = {
	type: 'walkRoad',
	accepts: (_world, entity) => !!entity.get(moving),
	start(world, entity, order) {
		order.state.leg = 0
		moveTo(world, entity, ROAD[0])
	},
	update(world, entity, order) {
		const status = entity.get(moving)!.status
		if (status === 'failed') return 'failed'
		if (status === 'moving') return 'running'
		order.state.leg++
		if (order.state.leg >= ROAD.length) return 'done'
		moveTo(world, entity, ROAD[order.state.leg])
		return 'running'
	}
}

Put it in Rules.orders and give it: world.issue({ type: 'order', player: 1, entities: [walker.id], order: { type: 'walkRoad' } }). packages/engine/tests/orders-api.test.ts walks a unit along a road with only the public API. See also Add an order.

Add a command ​

Commands of your own come from defineCommand (Economy: add a command).

Reference ​

OrderBehavior ​

FieldWhen
acceptsWhether a unit can take the order
startOnce, when it reaches the front of the queue
updateEvery tick; returns running, done or failed
endWhen it's over or interrupted

Move with moveTo(world, entity, point) and watch entity.get(moving).status (moving, arrived, failed); also halt, moveToEntity, faceTowards. Keep what the order needs to remember in order.state, so a save has it.