The initial numeric value for this observable.
Optionaloptions: ObservableOptions
Optional configuration for the observable, such as whether the value can be written by the client.
This function can't be called in restricted-execution mode.
early-execution-allowed - This function can be called in early-execution mode.
This function can't be called in restricted-execution mode.
early-execution-allowed - This function can be called in early-execution mode.
import { Player, system } from "@minecraft/server";
import { ObservableNumber } from "@minecraft/server-ui";
// Run `/scriptevent example:playerlevel` in chat to try this example.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:playerlevel" || !(sourceEntity instanceof Player)) return;
const currentLevel = new ObservableNumber(5);
const level = currentLevel.getData();
sourceEntity.sendMessage(`Current player level: ${level}`);
});
The new numeric value to set.
Updates the numeric value held by this observable. If the new value differs from the current value, all subscribed listeners are notified with the new value.
This function can't be called in restricted-execution mode.
early-execution-allowed - This function can be called in early-execution mode.
import { Player, system } from "@minecraft/server";
import { ObservableNumber } from "@minecraft/server-ui";
// Run `/scriptevent example:raisescore` in chat to try this example.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:raisescore" || !(sourceEntity instanceof Player)) return;
const score = new ObservableNumber(100);
score.subscribe((newScore) => {
sourceEntity.sendMessage(`Score changed to ${newScore}.`);
});
score.setData(250);
});
A function that receives the new numeric value each time the observable changes.
Registers a callback to be invoked whenever the observable's value changes. Returns the callback, which can be passed to unsubscribe to remove the listener.
This function can't be called in restricted-execution mode.
early-execution-allowed - This function can be called in early-execution mode.
import { Player, system } from "@minecraft/server";
import { CustomForm, ObservableNumber } from "@minecraft/server-ui";
const levels = [10, 20, 30, 40, 50];
// Run `/scriptevent example:healthtracker` in chat to open the form.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:healthtracker" || !(sourceEntity instanceof Player)) return;
const healthPercentage = new ObservableNumber(100);
// `subscribe` returns the callback. Keep it so `unsubscribe` can remove the same listener.
const listener = healthPercentage.subscribe((value) => {
sourceEntity.sendMessage(`Health updated to ${value} percent.`);
});
const form = new CustomForm(sourceEntity, "Health Tracker");
form.button("Unsubscribe", () => healthPercentage.unsubscribe(listener));
form.button("Subscribe", () => healthPercentage.subscribe(listener));
// Pick a random level every 2 seconds (40 ticks) so the listener receives changes.
const intervalId = system.runInterval(() => {
healthPercentage.setData(levels[Math.floor(Math.random() * levels.length)]);
}, 40);
// `show` resolves when the player closes the form. Stop the changes then.
form.show().then(() => system.clearRun(intervalId));
});
The callback handle previously returned by subscribe.
Removes a previously registered listener from this observable. Returns true if the listener was found and removed, false if it was not found.
This function can't be called in restricted-execution mode.
early-execution-allowed - This function can be called in early-execution mode.
import { Player, system } from "@minecraft/server";
import { CustomForm, ObservableNumber } from "@minecraft/server-ui";
const coinValues = [10, 25, 50, 75, 100];
// Run `/scriptevent example:bankvault` in chat to open the form.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:bankvault" || !(sourceEntity instanceof Player)) return;
const goldCoins = new ObservableNumber(50);
const listener = goldCoins.subscribe((coins) => {
sourceEntity.sendMessage(`Gold count: ${coins}`);
});
const form = new CustomForm(sourceEntity, "Bank Vault");
// `unsubscribe` removes the listener so it stops receiving changes.
form.button("Unsubscribe", () => goldCoins.unsubscribe(listener));
form.button("Subscribe", () => goldCoins.subscribe(listener));
// Pick a random coin count every 2 seconds (40 ticks) so the listener receives changes while subscribed.
const intervalId = system.runInterval(() => {
goldCoins.setData(coinValues[Math.floor(Math.random() * coinValues.length)]);
}, 40);
// `show` resolves when the player closes the form. Stop the changes then.
form.show().then(() => system.clearRun(intervalId));
});
An observable that holds a numeric value. Listeners are notified whenever the value changes.
Example: volumeControl.js
Source