Restart Entire Lens Behavior Script

Options

I wish there was a behavior script that can restart a lens experience. With one tap, or face trigger, the whole lens is back to start!

12
12 votes

Active · Last Updated

Comments

  • Gary Bartos
    Options

    If your script is set to OnAwake, could you have your script call a function that kicks off the functionality of your Lens? That can be an async call.

    Then you could have your restart experience--tap, face trigger, etc.--call that kickoff function (also asynchronously).

    So until there's a restart call, would that work for you?

    In the node tree, you might (?) have nodes disabled until your script enables them.

    Anyway, the technique of using event -> init function --> async call to kickoff function has worked well in other scenarios (e.g. industrial devices that NEED to restart cleanly), so some variant might work for you as well.

  • CyreneQ
    CyreneQ Posts: 5
    Options

    Hi Gary, thank you for this insight! Unfortunately, my coding knowledge isn't very advanced. I rely on behavior helper scripts.

    I usually have lenses that have multiple sequential tweens, triggers, animations, and I individually have to restart each tween and trigger. It would be convenient if there's a preset behavior script that just puts everything back in its place.

  • Kevando
    Kevando Posts: 76 🔥🔥
    Options

    Great question!

    Inside the music template lens I found some code that does just this and pulled it out into its own script because it's so helpful =) You can reset all the behaviors, AND your the "Play Automatically" tweens. That restartAutoTweens function is pretty dope because all you have to do is drop the object in there and it figures out the rest.

    // RestartHelperScripts.js
    
    // @input bool restartTweens 
    // @input bool resetTweens = true {"showIf" : "restartTweens", "label":"    Reset Tweens","hint": "This mostly effects tweens that use offsets"}
    // @input SceneObject[] tweenObjects {"showIf" : "restartTweens"}
    // @input bool restartBehavior {"hint" : "Behavior", "label": "Restart Behaviors"}
    // @input bool behaviorReinit = true {"label":"    Reinitialize","showIf" : "restartBehavior", "hint": "reinitialize all enabled behavior scripts."}
    // @input bool behaviorCallAwake = true {"label":"    On Awake","showIf" : "restartBehavior", "hint": "trigger onAwake Event once sceneObject is enabled"}
    // @input bool behaviorCallTurnOn = true {"label":"    On Start","showIf" : "restartBehavior", "hint": "trigger onStart Event once sceneObject is enabled"}
    
    function restartBehaviors() {
        if (global.behaviorSystem && script.restartBehavior) {
            if (script.behaviorReinit) {
                global.behaviorSystem.sendCustomTrigger("_reinitialize_all_behaviors");
            }
            if (script.behaviorCallAwake) {
                global.behaviorSystem.sendCustomTrigger("_trigger_all_awake_behaviors");
            }
            if (script.behaviorCallTurnOn) {
                global.behaviorSystem.sendCustomTrigger("_trigger_all_turn_on_behaviors");
            }
        }
    }
    
    function restartAutoTweens(so) {
        var scriptComponents = so.getComponents("Component.ScriptComponent");
        var tweenName;
        var tweenObject;
    
        for (var i = 0; i < scriptComponents.length; i++) {
            if (scriptComponents[i].playAutomatically == true) {
                tweenName = scriptComponents[i].api.tweenName;
                tweenObject = scriptComponents[i].api.tweenObject;
    
                // RESET!
                if (script.resetTweens) {
                    global.tweenManager.resetObject(tweenObject, tweenName);
                }
    
                // RESTART!
                global.tweenManager.startTween(tweenObject, tweenName);
            }
        }
        var childrenCount = so.getChildrenCount();
        for (var j = 0; j < childrenCount; j++) {
            restartAutoTweens(so.getChild(j));
        }
    }
    
    function restartTweens() {
        for (var i = 0; i < script.tweenObjects.length; i++) {
            var scriptObject = script.tweenObjects[i];
            restartAutoTweens(scriptObject);
        }
        print("here we go again");
    };
    
    script.api.restartEverything = function() {
        if (global.behaviorSystem && script.restartBehavior) {
            restartBehaviors();
        }
        if (script.restartTweens) {
            restartTweens();
        }
        print("Here we go again...");
    };
    
    
    
  • Anas Olabi
    Options

    Hi Kevando,
    i tried it .. but did do anything

    can you give me some hints please

    i created a button nd wanted if touched to restart the lens

  • Kevando
    Kevando Posts: 76 🔥🔥
    Options

    sure @Anas Olabi add me on discord

    kevando#3866