Script API - v1.26.10
    Preparing search index...

    Class Observable<T>Beta

    A class that represents data that can be Observed. Extensively used for UI.

    // Adapted from Microsoft's DDUI intro doc:
    // https://learn.microsoft.com/en-us/minecraft/creator/documents/scripting/intro-to-ddui
    import { CustomForm, Observable } from "@minecraft/server-ui";
    import { CommandPermissionLevel, CustomCommandStatus, Player, system } from "@minecraft/server";

    const statusLabel = Observable.create<string>("Ready");
    const itemCount = Observable.create<number>(0);

    // Whenever itemCount changes, the label text updates too.
    itemCount.subscribe((count) => {
    statusLabel.setData(`Found ${count} items`);
    });

    // Run /test:dynamiclabel in chat.
    system.beforeEvents.startup.subscribe((event) => {
    event.customCommandRegistry.registerCommand(
    {
    name: "test:dynamiclabel",
    description: "Show a form with a label bound to an Observable",
    permissionLevel: CommandPermissionLevel.Any,
    },
    (origin) => {
    const player = origin.sourceEntity;
    if (!(player instanceof Player)) {
    return {
    message: "This command can only be used by a player.",
    status: CustomCommandStatus.Failure,
    };
    }

    // Demo update so beginners can immediately see label changes.
    itemCount.setData(Math.floor(Math.random() * 10) + 1);

    CustomForm.create(player, "Inventory")
    .label(statusLabel)
    .button("Refresh", () => {
    itemCount.setData(Math.floor(Math.random() * 10) + 1);
    })
    .closeButton()
    .show()
    .catch((e) => {
    console.error(e);
    });

    return {
    message: "Opening dynamic label demo...",
    status: CustomCommandStatus.Success,
    };
    }
    );
    });
    import { CustomForm, Observable } from "@minecraft/server-ui";
    import { CustomCommandStatus, Player, system } from "@minecraft/server";

    const selectedOption = Observable.create<number>(0, { clientWritable: true });

    selectedOption.subscribe((newValue) => {
    console.log(`Selection changed to: ${newValue}`);
    });

    // Simulate a change from a player
    system.beforeEvents.startup.subscribe((event) => {
    event.customCommandRegistry.registerCommand(
    {
    name: "test:changeoption",
    description: "Change the selected option",
    permissionLevel: 0,
    },
    (origin) => {
    const player = origin.sourceEntity;
    if (!(player instanceof Player)) {
    return {
    message: "This command can only be used by a player.",
    status: CustomCommandStatus.Failure,
    };
    }
    system.run(() => {
    const form = CustomForm.create(player, "Change Option");
    form.dropdown("Select an option", selectedOption, [
    { label: "Option 1", value: 0 },
    { label: "Option 2", value: 1 },
    { label: "Option 3", value: 2 },
    ]);
    form.show().catch((e) => console.error(e));
    });
    return {
    message: "Creating form...",
    status: CustomCommandStatus.Success,
    };
    }
    );
    });
    import { Observable } from "@minecraft/server-ui";
    import { system, world } from "@minecraft/server";

    // Create an Observable to track the number of entities in the dimension
    const entityCount = Observable.create<number>(0);

    // Update the value - UI automatically reflects the change
    system.runInterval(() => {
    const entities = world.getDimension("overworld").getEntities();
    entityCount.setData(entities.length);
    }, 20);
    Important

    • Colour codes are only supported by the non-blurry Ore UI text (form title, header and label)
    • Glyphs are not supported by Ore UI at all

    Type Parameters

    Index

    Constructors

    Methods

    • Returns T

      Gets the data from the Observable.

      This function can't be called in early-execution mode.

    • Parameters

      • data: T

      Returns void

      Sets the data on this Observable and notifies the subscribers.

      This function can't be called in early-execution mode.

    • Parameters

      • listener: (newValue: T) => void

      Returns (newValue: T) => void

      Subscribes a callback to any changes that occur to the Observable. The return value can be passed into the unsubscribe function to stop listening to changes.

      This function can't be called in early-execution mode.

    • Parameters

      • listener: (newValue: T) => void

      Returns void

      Unsubscribe a callback from any changes that occur to the Observable. This takes the return value from the subscribe function.

      This function can't be called in early-execution mode.

    • Type Parameters

      Parameters

      Returns Observable<T>

      Creates an Observable, use this instead of a constructor.

      This function can't be called in early-execution mode.

      import { Observable } from "@minecraft/server-ui";

      // Read-only Observable (server controls the value)
      const status = Observable.create<string>("Loading...");

      // Client-writable Observable (UI controls can update it)
      const userInput = Observable.create<string>("", { clientWritable: true });