The initial boolean 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 { ObservableBoolean } from "@minecraft/server-ui";
// Run `/scriptevent example:autojumpstatus` in chat to try this example.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:autojumpstatus" || !(sourceEntity instanceof Player)) return;
const autoJump = new ObservableBoolean(true);
const isEnabled = autoJump.getData();
sourceEntity.sendMessage(`Auto jump is currently ${isEnabled ? "enabled" : "disabled"}.`);
});
The new boolean value to set.
Updates the boolean 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 { ObservableBoolean } from "@minecraft/server-ui";
// Run `/scriptevent example:turnonnotifications` in chat to try this example.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:turnonnotifications" || !(sourceEntity instanceof Player)) return;
const notifications = new ObservableBoolean(false);
notifications.subscribe((enabled) => {
sourceEntity.sendMessage(`Notification status changed to ${enabled}.`);
});
notifications.setData(true);
});
A function that receives the new boolean 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, ObservableBoolean } from "@minecraft/server-ui";
// Run `/scriptevent example:flightmonitor` in chat to open the form.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:flightmonitor" || !(sourceEntity instanceof Player)) return;
const flightMode = new ObservableBoolean(false);
// `subscribe` returns the callback. Keep it so `unsubscribe` can remove the same listener.
const listener = flightMode.subscribe((value) => {
sourceEntity.sendMessage(`Flight mode changed to ${value}.`);
});
const form = new CustomForm(sourceEntity, "Flight Monitor");
form.button("Unsubscribe", () => flightMode.unsubscribe(listener));
form.button("Subscribe", () => flightMode.subscribe(listener));
// Toggle the boolean every 2 seconds (40 ticks) so the listener receives changes.
const intervalId = system.runInterval(() => {
flightMode.setData(!flightMode.getData());
}, 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, ObservableBoolean } from "@minecraft/server-ui";
// Run `/scriptevent example:shieldstatus` in chat to open the form.
system.afterEvents.scriptEventReceive.subscribe(({ id, sourceEntity }) => {
if (id !== "example:shieldstatus" || !(sourceEntity instanceof Player)) return;
const shieldActive = new ObservableBoolean(true);
const listener = shieldActive.subscribe((value) => {
sourceEntity.sendMessage(`Shield active: ${value}`);
});
const form = new CustomForm(sourceEntity, "Shield Status");
// `unsubscribe` removes the listener so it stops receiving changes.
form.button("Unsubscribe", () => shieldActive.unsubscribe(listener));
form.button("Subscribe", () => shieldActive.subscribe(listener));
// Toggle the boolean every 2 seconds (40 ticks) so the listener receives changes while subscribed.
const intervalId = system.runInterval(() => {
shieldActive.setData(!shieldActive.getData());
}, 40);
// `show` resolves when the player closes the form. Stop the changes then.
form.show().then(() => system.clearRun(intervalId));
});
An observable that holds a boolean value. Listeners are notified whenever the value changes.
Example: nightVisionToggle.js
Source