1 Raycasting
Victor Westerlund edited this page 2026-11-15 13:31:29 +01:00

Raycasting

raycast:cast visits a bounded sequence of positions synchronously. The callback decides which blocks are passable, what a hit means, and whether to continue. The caster creates no entities and does not load or force-load chunks.

Arguments

Every argument is required, including args: {} when no callback data is needed.

Argument Type Meaning
steps Non-negative integer Maximum number of callback visits; zero performs no visits.
dx Number Local horizontal offset in the ^x direction, which is left relative to the execution rotation.
dy Number Local offset in the ^y direction.
dz Number Local forward offset in the ^z direction.
follow Integer 0 or 1 Choose the origin and rotation for offsets after a callback: the preceding command context (0) or the executor's current position and rotation (1).
f Function resource ID string Callback run at every sample.
args Compound Arguments passed to the callback; use {} for a non-macro callback.

Invalid bounds, follow modes, local-coordinate syntax, or non-compound args fail before a callback runs. Following mode also requires an entity executor. Provide an existing callback function and every macro argument it requires.

Positions and limits

The first sample is the caller's command position, before any step is taken. With five samples and a one-block step, the offsets are zero through four blocks. To skip the origin, position the call at the desired first sample:

execute positioned ^ ^ ^1 run function raycast:cast { steps: 5, dx: 0, dy: 0, dz: 1, follow: 0, f: "example:inspect", args: {} }

Use an explicit eye-relative position when starting from a player's eyes:

execute anchored eyes positioned ^ ^ ^0.25 run function raycast:cast { steps: 33, dx: 0, dy: 0, dz: 0.25, follow: 0, f: "example:inspect", args: {} }

Callbacks use feet anchoring, so an eye offset is not added again at each step. A zero step vector visits the same point repeatedly, still within steps. Large casts and their callbacks share Minecraft's function-command budget; choose a sample count appropriate to the work performed at each point.

Callback context and continuation

Callbacks retain the caller's executor (@s) and run at the sampled position in the current dimension. Only the fields in args are supplied as macro arguments.

  • return 1 continues to the next sample if any budget remains.
  • Any other result, including return 0, return fail, or no explicit return, stops the cast after the current sample.
  • The stopping sample counts toward the result.

Example callback, example:inspect:

# Continue through air without producing hit effects.
execute if block ~ ~ ~ minecraft:air run return 1
execute if block ~ ~ ~ minecraft:cave_air run return 1
execute if block ~ ~ ~ minecraft:void_air run return 1

# Show the first obstruction and finish traversal.
particle minecraft:end_rod ~ ~ ~ 0 0 0 0 1
return 0

The caster returns the number of callback visits, including a visit that stopped traversal. This is an integer result, not mcfstd's boolean output register:

execute store result score @s inspected_points run function raycast:cast { steps: 32, dx: 0, dy: 0, dz: 0.25, follow: 0, f: "example:inspect", args: {} }

Following an executor

follow: 0 steps from the preceding command position and rotation. Moving the executor inside the callback does not move the next sample's origin.

follow: 1 steps from the executor's position and rotation after the callback. The callback should move the executor to the sampled position when using it as a cursor, for example with teleport @s ~ ~ ~. Any additional callback movement then affects the next step. Keep that executor valid in the same dimension while continuing; stop traversal when removing it or transferring it to another dimension. The first sample still comes from the caller's command position in either mode.

Nested calls and consumers

A callback can invoke raycast:cast again. Each invocation snapshots its arguments and budget on a private stack; returning from a child restores the parent's frame. All casts complete in the same function call chain rather than scheduling work.

mcfstd's placement detector uses 33 samples from 0.25 through 8.25 blocks ahead, with separate handling for all three air types. Bomberman uses entity-following casts so its sand callback can shift the ray cursor. Its direction offsets, explosion markers, passable-block policy, and visual effects remain game-owned.