#Unsubscribe an event

1 messages · Page 1 of 1 (latest)

tame veldt
#

How can i unsubscribe an event in scripting?

smoky atlas
#

?

#

More information pls

raw grotto
#
const event = world.afterEvents.playerPlaceBlock.subscribe((data) => {
    console.warn("Broke a block.");
    world.afterEvents.playerPlaceBlock.unsubscribe(event);
});
raw grotto
#

Yes?

tame veldt
#

Isn't the return value of subribe an void?

#

so event would be undefined

raw grotto
#

No?

#

It doesn’t return void afaik

tame veldt
#

thank you

raw grotto
#

yay!! :D

split condor
tacit apex
leaden glen
raw grotto
# split condor what about to re-subscribe to that event?

You could take the callback function and define it elsewhere and then provide it when subscribing

function callback(data) {
    console.warn("Broke a block.");
};

const event = world.afterEvents.playerPlaceBlock.subscribe(callback);
world.afterEvents.playerPlaceBlock.unsubscribe(event);
split condor
raw grotto
split condor
#

Example

raw grotto
#
/**
 * @param { import("@minecraft/server").PlayerPlaceBlockAfterEvent } data
 */
function callback(data) {

};
split condor
raw grotto
split condor
#

I do not want to use typescript and have to go thru an extra step to test... and I have not finished my regolith filter to do this.... LOL

#

I like being extra careful in JS

raw grotto
split condor
split condor