1. Wiki
  2. /Commands
  3. /Functions and command blocks

Functions and command blocks

Bundle Minecraft Bedrock commands into .mcfunction files, run them with /function or every tick, and use impulse, chain and repeat command blocks.

Typing the same ten commands over and over gets old fast. A function is a text file full of commands that runs all of them at once with a single /function call. Functions can also run by themselves on every game tick, which is how many add-ons make things happen all the time.

If commands are new to you, read Commands basics first.

The quick way: GodCraft

GodCraft has a Function object that writes the files and the tick list for you.

  1. Open the Workshop and add a Function to your add-on.
  2. Type one command per line. Hints suggest commands and arguments as you go.
  3. Choose how it runs: every tick, once on world load, or only by /function.
  4. Export. The .mcfunction file, tick.json, manifests and UUIDs are all written for you.

By hand

Step 1: create a function file

Functions live in the behavior pack (BP), inside a folder named functions. The file ends in .mcfunction and is plain text: you can write it in any text editor.

BP/functions/myname/starter_kit.mcfunction
# Gives whoever runs this function a starter kit
give @s minecraft:stone_sword
give @s minecraft:bread 8
give @s minecraft:torch 16
effect @s minecraft:speed 60 0
say Your starter kit is ready!

The rules are simple:

  • One command per line. A command cannot be split across two lines.
  • No slash. Write give, not /give.
  • Lines starting with # are comments. The game skips them. Use them to explain what the function does.
  • Empty lines are fine and make long files easier to read.

Use a folder named after you

Putting your files in a folder like functions/myname/ keeps them from clashing with other add-ons that might also have a starter_kit function.

Step 2: run it

Load a world with your behavior pack on and cheats enabled, then type:

Chat
/function myname/starter_kit

The name is the path inside the functions folder, using / between folders and without the .mcfunction ending. See /function.

Inside a function, @s and ~ ~ ~ mean whoever ran /function and where they stand. So when you run the starter kit, you get the items.

Nesting folders

You can sort functions into as many folders as you like:

Folder layout
BP/functions/
  tick.json
  myname/
    starter_kit.mcfunction
    tick.mcfunction
    arena/
      start.mcfunction
      reset.mcfunction

Run the nested one with /function myname/arena/start.

Functions can also call other functions. This is great for splitting a big job into small parts:

BP/functions/myname/arena/start.mcfunction
# Clean up, then teleport everyone in
function myname/arena/reset
tp @a 0 80 0
title @a title Fight!

Do not call yourself

A function that calls itself, directly or through another function, makes an endless loop. The game cuts it off after too many commands, so the result will not be what you expect.

Step 3: run every tick with tick.json

Minecraft updates the world 20 times per second. Each update is a tick. To run a function on every tick, list it in tick.json directly inside the functions folder:

BP/functions/tick.json
{
  "values": [
    "myname/tick"
  ]
}

Each entry in values is a function path, written the same way as with /function. Now write the tick function:

BP/functions/myname/tick.mcfunction
# Players standing on a gold block get Speed
execute as @a at @s if block ~ ~-1 ~ minecraft:gold_block run effect @s minecraft:speed 1 1 true

This checks every player 20 times per second. Anyone standing on a gold block gets a short Speed boost, which keeps refreshing while they stay there. The true at the end hides the effect particles.

Who is @s in a tick function?

Tick functions are run by the game itself, not by a player, so @s points at nobody. Use /execute with as @a or as @e[...] to run commands for each player or mob, like the example above.

Keep tick functions light. Something that runs 20 times a second on every player adds up quickly. Narrow your selectors with arguments like type, r or tag.

Reloading changes

After you edit a function file, you do not need to restart the game. Run /reload to load the new versions of functions and scripts. If a function has a broken command, the whole file may not load, so check the content log for errors. See Troubleshooting.

Command blocks

Command blocks run a command when they get power, right inside your world. They are perfect for maps, minigames and testing. Functions are better for add-ons, because they travel with your pack instead of living in one world.

Getting one

You cannot find command blocks in the creative inventory. Use:

Chat
/give @s minecraft:command_block

Place it, then tap or right-click it to open its settings. Type a command in the box, without the slash.

The three types

TypeColorWhat it does
ImpulseOrangeRuns once each time it is powered
ChainGreenRuns right after the block pointing into it runs
RepeatPurpleRuns every tick while it is powered

Main settings

  • Block Type: Impulse, Chain or Repeat.
  • Condition: Unconditional always runs. Conditional only runs if the block behind it succeeded.
  • Redstone: Needs Redstone waits for a signal from a button, lever or redstone. Always Active runs without one.
  • Delay in Ticks: waits this many ticks before running.

Example: a teleport button

  1. Place an Impulse command block set to Needs Redstone, with: tp @p 0 100 0
  2. Put a stone button on it.
  3. Press the button: the nearest player flies to 0 100 0.

Example: a chain

Command blocks have an arrow on their sides showing which way they point. A chain block placed where the arrow points will run next.

  1. Repeat, Always Active: testfor @a[r=5]
  2. A Chain block in front of it, Conditional, Always Active: effect @a[r=5] minecraft:regeneration 2 0 true

Now any player within 5 blocks gets healed, but only while someone is nearby. /testfor succeeds if it finds someone, and the conditional chain block only runs on success.

Function or command block?

A command block can also run a whole function: put function myname/arena/start in it. That way the logic lives in your add-on and the block is just the button.

What next

Commands: Guides

Commands basics ·Functions and command blocks

We use cookies to improve your experience and analyse site traffic. Learn more