Created
May 18, 2022 19:55
-
-
Save azerum/151ec67dc474320b28d71a69d1742953 to your computer and use it in GitHub Desktop.
Helper functions to work with triggers in Lens Studio
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // -----JS CODE----- | |
| //NOTE: | |
| //To this scipt to work you need to: | |
| // - Put 'Behavior' script on the very top of Objects hierarchy. Use | |
| // + > Helper Scripts > Behavior to add it. You can leave that script | |
| // to work 'On Awake' | |
| // - Put this script on top of the Objects hierarchy, AFTER behavior. Bind | |
| // this script to 'On Start', NOT 'On Awake' | |
| /** | |
| * Sends the trigger with specified name | |
| * | |
| * @param {string} triggerName | |
| */ | |
| global.send = function(triggerName) { | |
| global.behaviorSystem.sendCustomTrigger(triggerName); | |
| }; | |
| /** | |
| * Subscribes a `handler` function to the trigger with name | |
| * `triggerName`. `handler` will be called every time somebody | |
| * sends the trigger with name `triggerName` | |
| * | |
| * @param {string} triggerName | |
| * @param {() => void} handler | |
| */ | |
| global.subscribe = function(triggerName, handler) { | |
| global.behaviorSystem.addCustomTriggerResponse(triggerName, handler); | |
| }; | |
| /** | |
| * Unsubscribes `handler` from the trigger with name `triggerName`. | |
| * Note that `handler` must be *exactly same* function object as the one | |
| * that was passed to `global.subscribe()` | |
| * | |
| * @param {string} triggerName | |
| * @param {() => void} handler | |
| */ | |
| global.unsubscribe = function(triggerName, handler) { | |
| global.behaviorSystem.removeCustomTriggerResponse(triggerName, handler); | |
| }; | |
| /** | |
| * Subscribes to the trigger with `triggerName`, and unsubscribes | |
| * from it once it has happened. That is, `handler` function will be called | |
| * only once - on the first occurence of the trigger | |
| * | |
| * @param {string} triggerName | |
| * @param {() => void} handler | |
| */ | |
| global.subscribeOnce = function(triggerName, handler) { | |
| var callback; | |
| callback = function() { | |
| handler(); | |
| global.unsubscribe(triggerName, callback); | |
| }; | |
| global.subscribe(triggerName, callback); | |
| }; | |
| /** | |
| * Subscribes to the all triggers from the `triggers` array. Once any of | |
| * the triggers has happened, runs that `handler` function and | |
| * unsubscribes from fursther occurences of all the triggers. | |
| * | |
| * That is, `handler` will be called only once - once any of the triggers | |
| * from the `triggers` array has happened. It can be any trigger - whichever | |
| * happened first. | |
| * | |
| * Handler can optionally take one parameter - the name of the trigger that | |
| * caused `handler` to run | |
| * | |
| * @param {string[]} triggers | |
| * @param {(triggerName?: string) => void} handler | |
| */ | |
| global.subscribeOnceAny = function(triggers, handler) { | |
| var unsubscribers = []; | |
| var unsubscribeFromAll = function() { | |
| unsubscribers.forEach(function(u) { | |
| u(); | |
| }); | |
| }; | |
| triggers.forEach(function(triggerName) { | |
| var callback = function() { | |
| handler(triggerName); | |
| unsubscribeFromAll(); | |
| }; | |
| global.subscribe(triggerName, callback); | |
| unsubscribers.push(function() { | |
| global.unsubscribe(triggerName, callback); | |
| }); | |
| }); | |
| }; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment