#Unsubscribe an event
1 messages · Page 1 of 1 (latest)
const event = world.afterEvents.playerPlaceBlock.subscribe((data) => {
console.warn("Broke a block.");
world.afterEvents.playerPlaceBlock.unsubscribe(event);
});
wow thank you a lot
but
Yes?
oh well it works. I think my ide is just giving me wrong information about the api
thank you
yay!! :D
what about to re-subscribe to that event?
It's the callback that tells it returns void, not the subscribe method. Subscribe returns the callback.
re-subscribe uses subcribe()
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);
The only thing about completely doing it this way, is that you loose the intellisense unless you do an if not an instanceof....
I got it working, so yes it works with re-subscribing with the const
You can also use JSDoc instead of instanceof
Example
/**
* @param { import("@minecraft/server").PlayerPlaceBlockAfterEvent } data
*/
function callback(data) {
};
works like that in the comments... is there an extension or something I need?
They should work in Visual Studio Code by default, you won’t need any extensions
and can it work for other data types? I have been using a default when I can to help it have context.
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
Yeah
You might find this website useful if you want to know more about it
Official documentation for JSDoc.
Awesome Sauce.... thanks KING.
F'N' gamechanger..... is this in script resources... just the @param part... would help so many...