Bedrock Wiki
  • Discord
  • Contribute
  • bedrock.dev
  • MS Learn
Beginner's Guide
  • Guide
    • 1. Introduction
      guide
    • 2. Add-Ons Explained
    • 3. Software & Preparation
    • 4. Project Setup
    • 5. Create a Custom Item
    • 6. Create a Custom Entity
    • 7. Blockbench: Modeling, Texturing & Animating
    • 8. Adding Loot Tables, Spawn Rules & Crafting Recipes
  • Extra
    • a. Understanding JSON
    • b. Download Example Packs
    • c. Troubleshooting
      help
    • d. Advanced Manifest
    • e. Format Versions
    • f. Project Setup Android
Animation Controllers
  • Intro to Animation Controllers
    guide
  • Entity Commands
  • AFK Detector
  • Death Commands
  • Molang into Scoreboard
  • Respawn Commands
Blocks
  • General
    • Intro to Blocks
      guide
    • Block Components
    • Block Tags
    • Block States
    • Block Traits
    • Block Permutations
    • Block Events
      Scripts
    • Block Event Migration
      help
    • Blocks as Items
    • Multi-Blocks
    • Troubleshooting Blocks
      help
  • Sound & Visuals
    • Intro to Block Visuals
      guide
    • Block Culling
    • Block Models
      guide
    • Block Sounds
    • Block Texture Animation
    • Block Texture Variation
    • Block Tinting
  • Tutorials
    • Applying Effects
      Scripts
    • Avoiding State Limit
    • Block Orientation
    • Fake Blocks
      Scripts
    • Intercardinal Orientation
      Scripts
    • Ore Loot Tables
      Scripts
    • Tool-Based Destruction
  • Vanilla Re-Creations
    • Custom Crops
      Scripts
    • Custom Glass
    • Custom Glazed Terracotta
    • Custom Heads
      Scripts
    • Custom Trapdoors
      Scripts
  • Documentation
    • Block Co-Location
    • Block Format History
    • Block Shapes
    • Redstone Components
    • Vanilla Block Models
    • Vanilla Block Sounds
    • Vanilla Block Tags
    • Vanilla Voxel Shapes
Commands
  • General
    • Intro to Command Blocks
    • Understanding Selectors
    • Functions
      guide
    • Block States
    • Coordinate System
    • Giving NBT Items
    • Scoreboard Operations
  • Commands
    • Damage
    • Execute
    • Playanimation
    • Playsound
  • On Event Systems
    • On First World Load
      function
    • On Player First Join
    • On Player Join
    • On Player Leave
    • On Player Death
    • On Player Respawn
  • Scoreboard Systems
    • Entity Counter
    • Scoreboard Timers
    • Comparing And Retrieving Scores
  • Detection Systems
    • Look Detection
    • Item Drop Detection
    • Movement Detections
    • Rain Detection
  • Techniques
    • Execute Logic Gates
      concept
    • Execute at Block Variant
    • Execution Forking
      concept
    • Binary in Commands
      concept
    • MBE - Max's Block Entity
    • FMBE - A New Way to Create Display Entities
    • Dynamic Displays
    • Orbital Camera
  • Useful Creations
    • Sphere Command
    • Compass Display
    • Custom Crafting
    • Multiplayer Position Rearrangement
      function
Concepts
  • contents.json
  • Molang
  • Namespaces
  • Overwriting Assets
  • Shaders
  • Sounds
  • Subpacks
  • Texture Atlases
  • textures_list.json
Documentation
  • Advanced Molang
  • File Types
  • Fog IDs
  • Material Configuration Description
  • Molang Queries
  • Pack Folder Structure
  • Shared Constructs
  • Sound Definitions
  • Vanilla Materials
Entities
  • General
    • Intro to Entities BP
      guide
    • Intro to Entities RP
      guide
    • Troubleshooting Entities
      help
    • Entity Events
    • Entity Properties
    • NPC Dialogue
    • Render Controllers
    • Spawn Rules
  • Tutorials
    • Area Effect Clouds
    • Coordinate Space Conversion
    • Creating Boats
    • Detecting Other Entities
    • Disabling Team Damage
    • Dummy Entities
    • Entity Attacks
    • Entity Movement
    • Entity Timers
    • Holding Items
    • Invulnerable Entities
    • Look at Entity
    • Riding Flying Entities
    • Sleeping Entities
    • Solid Entities
    • Spawning Tamed Entities
      Scripts
    • Village Mechanic
  • Documentation
    • Dummy Components
    • Non-Mob Runtime Identifiers
    • Projectiles
    • Runtime Identifiers
    • Vanilla Usage Components
    • Vanilla Usage Spawn Rules
Items
  • General
    • Intro to Items
      guide
    • Item Components
    • Item Tags
    • Item Events
      Scripts
    • Item Event Migration
      help
    • Item Catalog
    • Troubleshooting Items
      help
  • Tutorials
    • Equipment-Based Commands
    • Spawning Items
    • Throwable Items
  • Vanilla Re-Creations
    • Custom Armor
    • Custom Food
      Scripts
    • Custom Pottery Sherds
    • Custom Weapons
  • Documentation
    • Enchantments
    • Attachables
    • Item Format History
    • Numerical Item IDs
    • Vanilla Item Groups
    • Vanilla Item Tags
    • Vanilla Usage Components
JSON UI
  • General
    • Intro to JSON UI
      guide
    • Best Practices
      guide
  • Tutorials
    • Adding HUD Elements
    • Aseprite Animations
    • Buttons and Toggles
    • Dynamic Content Generation
    • Modifying Server Forms
    • Preserve Title Texts
    • String to Number
  • Documentation
    • JSON UI Documentation
Loot, Recipes & Trading
  • General
    • Trading Behavior
  • Documentation
    • Loot Tables
    • Trade Tables
    • Recipes
    • Item Functions
  • Tutorials
    • Randomized Structure Loot
Meta
  • Add-On Performance
  • Style Guide
  • Useful Links
  • Using Schemas
  • Version Control
  • Q&A
    • Blocks and Items Q&A 2024/08/30
    • Deferred Technical Preview Q&A 2024/02/23
    • GameTest Q&A 2021/08/06
    • Scripting and Editor Q&A 2023/09/22
    • World Generation Q&A 2024/11/15
NBT
  • General
    • .mcstructure
  • Tutorials
    • Enabling Experiments by Editing NBT
    • Extending Structure Limits
  • NBT in Depth
    • About NBT (Named Binary Tag)
    • NBT Libraries
    • Reading NBT Example
Particles
  • General
    • Intro to Particles
      guide
  • Tutorials
    • Disabling Particles
  • Documentation
    • Vanilla Particles
Scripting
  • General
    • Intro to Scripting
    • What is Script API?
    • API Modules
  • Tutorials
    • Script Core Features
    • Block Placement Prevention
    • Custom Commands
    • GameTests
    • Script Privileges
    • Script Requests API
    • Server Forms
  • Documentation
    • Engine Environment
    • Script Resources
    • Script Watchdog
    • Troubleshooting JavaScript
    • TypeScript
Servers
  • Software
    • Bedrock Server Software
  • Protocols
    • Bedrock Protocol
    • NetherNet Protocol
    • RakNet Protocol
Text & Localization
  • General
    • Intro to Text & Localization
    • Raw Text
    • Splashes
  • Emojis & Symbols
    • Fonts
    • Intro to Emojis
    • Custom Emojis
    • Input Keys
Visuals
  • General
    • Introduction to Entity Visuals
      guide
    • Bedrock Modeling
    • Custom Death Animations
    • Effects in Animations
    • Material Creations
    • Materials
    • Math-Based Animations
    • Skin Packs
  • Tutorials
    • Entity Texture Animation
    • Glowing Entity Texture
    • Hurt Animations
    • Leash Position
    • Player Geometry
    • Remove Entity Shadows
    • Retexturing Spawn Eggs
  • Ideas
    • Structure Presentation
World Generation
  • General
    • Intro to World Generation
      guide
    • Biomes
      guide
    • Feature Types
    • Jigsaw Structures
  • Tutorials
    • Block Conditions for Features
    • Generating Custom Ores
    • Generating Custom Structures
    • Generating Patches
    • Heightmap Noise
  • Documentation
    • Biome Tags

Dynamic Content Generation

intermediate
Dynamic Content Generation
  • Introduction
  • Factories
    • Key properties
    • Usage
  • Grids
    • Key properties
    • Usage
    • Grid Position
  • Useful Information

WARNING

This tutorial assumes that you have a basic understanding of the structure of JSON-UI and have familiarity with bindings and collections. If you aren't familiar, you may check out Intro to JSON UI and JSON UI Documentation first.

You also need to know how to add elements to a screen's entry point to use this code. Refer to Adding HUD Elements.

Introduction ​

JSON UI allows dynamic generation of UI elements through the use of factories and grids. This is useful when you don't know how many elements the UI should have beforehand. This tutorial focuses on the use of both factories and grids.

INFO

Throughout this tutorial, the terms "control" and "element" are used synonymously.

Factories ​

A factory generates UI elements specified in its control_name or control_ids properties.

  • Invocation: Many of the vanilla factories are invoked natively by the engine. Alternatively, factories can be invoked manually by overriding the #collection_length property.
  • Data Binding: For the use of #collection_length, the factory must reside within a collection-supporting container (e.g., collection_panel or stack_panel). The value can either be an int or a string[] based on the implementation of the factory.

Key properties ​

PropertyTypeShort Description
namestringThe unique identifier for the factory. This is critical for factories natively invoked by the engine, though custom factories can use any arbitrary string.
control_namestringReference to the control to be generated by the factory
control_idsObjectAn object mapping arbitrary string keys to the references of the controls to be generated. Used when generating multiple distinct controls. Must remain unchanged for natively invoked factories.
factory_variablesstring[]List of variables that should be passed to the controls generated by the factory
max_children_sizeintMaximum number of controls to be generated

Usage ​

Using control_name: ​

In this case, #collection_length must be overridden with an int value. This is the number of elements that will be generated.

📝 RP/ui/hud_screen.json
json
{
  "hud_apple": {
    "type": "image",
    "texture": "textures/items/apple",
    "size": [32, 32]
  },

  "apple_factory": {
    "type": "stack_panel",
    "size": [ "100%cm", "100%c" ],
    "factory": { // Defined in a stack_panel (Supports collection)
      "name": "hud_apple_factory",
      "control_name": "hud.hud_apple" // Reference to hud_apple
    },
    "bindings": [
      {
        "binding_name": "#hud_title_text_string",
        "binding_name_override": "#count"
      },
      {
        "binding_type": "view",
        "source_property_name": "(#count - 'Count:' + 0)", // Gets the int value
        "target_property_name": "#collection_length" // Overriding #collection_length
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27

Here, an integer value is sent through the title as Count:{value} that indicates the number of elements to generate. The bindings listed above first fetch the title string and store it in #count. Then, #count - 'Count:' gets the int value and #collection_length is overridden with this value. Note: + 0 sets a default value of 0 if the string is empty.

Expected result: For title: Count:3

control-name-result

Using control_ids: ​

In this case, #collection_length must be overridden with an array of strings. The strings are the keys defined in control_ids. There's not much freedom with this approach as there is no way to manipulate arrays in JSON UI. Instead, we can pre-define some arrays within the property_bag and choose from them. The elements will be generated in the order specified in the array.

📝 RP/ui/hud_screen.json
json
{
  "hud_apple": {
    "type": "image",
    "texture": "textures/items/apple",
    "size": [32, 32]
  },

  "hud_label": {
    "type": "label",
    "text": "Apple!",
    "shadow": true
  },

  "apple_factory": {
    "type": "stack_panel",
    "size": [ "100%cm", "100%c" ],
    "factory": {
      "name": "hud_apple_factory",
      "control_ids": {
        "apple": "hud.hud_apple", // An arbitrary name mapped to the reference of a control
        "label": "hud.hud_label"
      }
    },
    "property_bag": {
      // Some pre-defined arrays
      "#list1": [ "apple", "label" ],
      "#list2": [ "label", "apple" ]
    },
    "bindings": [
      {
        "binding_name": "#hud_title_text_string",
        "binding_name_override": "#choice"
      },
      {
        "binding_type": "view",
        "source_property_name": "('#list' + #choice)", // Constructing key name
        "target_property_name": "#collection_length"
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41

Here, we have defined two controls in control_ids and two arrangements of these controls as #list1 and #list2. In the bindings, we fetch the title string and store it in #choice, which is an integer representing the list to be selected. Then using ('#list' + #choice), we construct the name of the list and that list is then overridden to #collection_length.

Expected result: For title: 1 and 2 respectively

factory-control-ids-result   factory-control-ids-result

Factory (Extra): ​

You can define factories directly using "type": "factory" as well. These factories are always invoked natively by the engine. However, it is possible to define these factories in the ways discussed above we can also modify the control references to generate custom elements instead.

📝 RP/ui/server_form.json
json
{
  "main_screen_content": { // Vanilla code from server_form.json
    "type": "panel",
    "size": [0, 0],
    "controls": [
      {
        "server_form_factory": { // Name of control here acts as the factory name and thus should remain unchanged
          "type": "factory",
          "control_ids": {
            "long_form": "@server_form.long_form",
            "custom_form": "@server_form.custom_form"
          }
        }
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17

Grids ​

Grids generate and arrange UI elements in a grid layout. The elements are generated from a template control specified in its grid_item_template property. We can either generate a fixed number of elements by defining grid_dimensions or a variable number by overriding its #maximum_grid_items binding or maximum_grid_items property.

Key properties ​

PropertyTypeShort Description
grid_dimensionsVector [columns, rows]Number of columns and rows the grid should have. Can generate a maximum of columns × rows elements
grid_dimension_bindingstringBinding name that provides dimensions for the grid. Only takes hardcoded bindings.
grid_item_templatestringReference to the control to be generated by the grid
grid_rescaling_typeenumCan be none, horizontal, vertical
maximum_grid_itemsintMaximum number of controls to be generated
Corresponding binding #maximum_grid_items
grid_positionVector [row, column]Position of a control inside a grid

DANGER

Try not to use vertical as the value of grid_rescaling_type. It may crash your game.

Usage ​

INFO

Grids typically have a default size of ["100%c", "100%c"]. It is ideal to keep the height dynamic (100%c). If a fixed height is applied, generated elements may overlap if the height is too small, or have unintended gaps if the height is too large.

Using grid_dimensions: ​

It is recommended not to override the size property in this case, as some elements can go outside the grid bounds and mess up the layout.

📝 RP/ui/hud_screen.json
json
{
  "hud_apple": {
    "type": "image",
    "texture": "textures/items/apple",
    "size": [32, 32]
  },

  "apple_grid": {
    "type": "grid",
    "grid_dimensions": [3, 3],
    "grid_item_template": "hud.hud_apple"
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13

Expected result:

grid-dimension-result

Overriding #maximum_grid_items: ​

For this to work properly, you have to modify the width of the grid. This is because the number of columns in this case will be floor( width of grid / width of template control ). Moreover, we need to set grid_rescaling_type to horizontal. Then we have to override #maximum_grid_items through bindings, or alternatively, define the maximum_grid_items property.

📝 RP/ui/hud_screen.json
json
{
  "hud_apple": {
    "type": "image",
    "texture": "textures/items/apple",
    "size": [32, 32]
  },

  "apple_grid": {
    "type": "grid",
    "size": [160, "100%c"], // floor(160 / 32) = 5; Meaning the grid will have 5 columns
    "grid_item_template": "hud.hud_apple",
    "grid_rescaling_type": "horizontal",
    "bindings": [
      {
        "binding_name": "#hud_title_text_string",
        "binding_name_override": "#count"
      },
      {
        "binding_type": "view",
        "source_property_name": "(#count - 'Count:' + 0)", // Gets the int value
        "target_property_name": "#maximum_grid_items" // Overriding #maximum_grid_items
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Here, an integer value is sent through the title as Count:{value} that indicates the number of elements to generate. The bindings listed above first fetch the title string and store it in #count. Then, #count - 'Count:' gets the int value and #maximum_grid_items is overridden with this value.

Expected result: For title: Count:15

grid-max-items-result

Grid Position ​

There might be situations where you'd want to manually add controls to the grid instead of generating them from a template. With the grid_position property you can place your control anywhere on the grid.

Usage ​

Let's say we have a grid with 2 columns and 2 rows and we want to place our controls in this way-

Apple

[0, 0] - 0
[0, 1] - 1
[1, 0] - 2

Apple

[1, 1] - 3

The format [row, column] indicates the placement within the grid, where the first integer is the row index and the second integer is the column index. The following integer is the index it takes inside the grid for a defined collection.

The code:

📝 RP/ui/hud_screen.json
json
{
  "hud_apple": {
    "type": "image",
    "texture": "textures/items/apple",
    "size": [32, 32]
  },

  "apple_grid": {
    "type": "grid",
    "size": ["100%c", "100%c"], // Define it explicitly to avoid layout issues
    "grid_dimensions": [2, 2],
    "controls": [
      { 
        "[email protected]_apple": {
          "grid_position": [0, 0]
        }
      },
      {
        "[email protected]_apple": {
          "grid_position": [1, 1]
        }
      }
    ]
  }
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25

Expected result:

grid-position-result

Useful Information ​

  • When used with a collection, Factories and Grids automatically index generated elements.
  • Generated elements can access outside bindings via source_control_name, but outside elements cannot access bindings from generated elements.
  • Invisible elements inside a Grid still occupy physical space, which can leave unintentional gaps in your UI.
  • In Grids, animations linked with the next property can appear bugged. After the first animation, the duration divides among the grid elements, causing subsequent animations to speed up. (flip_book animations are the only exception).

Contributors

Edit Dynamic Content Generation on GitHub

Bedrock Wiki by Bedrock OSS

"Minecraft" is a trademark of Mojang AB.

Bedrock OSS, Bedrock Wiki and bedrock.dev are not affiliated in any way with Microsoft or Mojang AB.

  • Privacy Policy
  • Join our Discord
  • Learn how to Contribute
  • Visit our Repository