The forum on this site is closed for new comments and posts. Continue the conversation in our Snap AR Discord server.

Reset Front and Back lenses

  • Hi again,

     

    Does anyone have any thoughts on the resetting of games mechanics as per my original question?

     

    Many thanks,

     

    Rich

    Comment actions Permalink
  • Hi Richard!

    The most important thing to note is that you'd probably want to have a separate object to be your game controller that is not affected by flipping the camera front to back.

    The idea here is that if you disable the object, its script won't run. So if you try to disable the back camera object, and its script is on that disabled object, then whatever logic you put in there won't run.

    For example, here's an example logic, where on the back camera, the Lens prints an increasing counter every frame. If you switch to front camera, this counter will stop, and the counter will reset to 0.

    Can't wait to see your final game!

    Cheers,

    Jonathan 

    // -----JS CODE-----
    // @input SceneObject frontCamera
    // @input SceneObject backCamera

    var counter = 0;
    var isBackCamera;

    script.createEvent("CameraBackEvent").bind(function() {
    print('go to back camera');
    isBackCamera = true;
    script.frontCamera.enabled = false;
    script.backCamera.enabled = true;
    })

    script.createEvent("CameraFrontEvent").bind(function() {
    print('go to front camera');
    isBackCamera = false;
    script.frontCamera.enabled = true;
    script.backCamera.enabled = false;

    counter = 0;
    })

    script.createEvent("UpdateEvent").bind(function() {
    if (isBackCamera) {
    counter++;
    print(counter)
    }

    })
    Comment actions Permalink
  • Thank you Jonathan for your response.

    If it’s ok I wanted to show you in a little more detail so I can get this right as I feel it’s a bit more complicated. Or maybe it isn’t!

    So, I have this set up. Various cameras are performing various things. The main thing is that when I go from front lens to back lens the game has been playing in the background and the countdown is still counting down. I want to completely reset to its original state when the lens was initially launched.

    Below are the cameras and objects within my project. ‘Game mechanics_Back Camera’ is where all the functions for my Whack A Mole styled game are held.

    Here is the open ‘Game mechanics_Back Camera’ object. There are objects for the game, lighting, an animated countdown, an update score at and of countdown and the simple high score controller.

    Within in this is an object with further controls ‘Recycle_Bin_Behaviour’. This houses a number of holes which instantiate objects into the scene at given times throughout the game. When the countdown ends, the instantiated objects are timed to end just before that.

    Here is an example of the instantiation scripts on Hole_1. As you can see, once the script is activated to start the behaviour, it runs through the events. They have been triggered by the initial touch event of a button at the start of the game. The countdown is also triggered by this button.

    So, with your script – could you help understand how to apply to this logic? In my head, once the scripts have been initialized they are off and running. Basically, how do you reset all of this (including scripts running and tween that have run) to their original state. As mentioned previously I’m not a coder, so it’s taking me a little time to get my head around this!

    Many thanks again,

    Rich

    Comment actions Permalink
  • Hey there, just wondering if anyone might have a solution to the above? Would greatly appreciate some help in this matter.

    Comment actions Permalink
  • Hi Richard,

    That looks like an impressive game setup! 

    To reset the tween and behavior, you can use 

    //@input bool  tweenReset = true {"label":"Reset Tweens"}
    //@input bool tweenPlayAutomatic = true {"label":"Play Automatic", "showIf": "tweenReset"}
    //@input bool behaviorReinit = true {"label":"Reinitialize"}
    //@input bool behaviorCallTurnOn = true {"label":"Trigger Turn On Event"}

    function resetBehaviorTween() {
    if (global.behaviorSystem) {
    if (script.behaviorReinit) {
    global.behaviorSystem.sendCustomTrigger("_reinitialize_all_behaviors");
    }
    if (script.behaviorCallTurnOn) {
    global.behaviorSystem.sendCustomTrigger("_trigger_all_turn_on_behaviors");
    }
    }

    if (global.tweenManager) {
    if (script.tweenReset) {
    global.tweenManager.resetTweens();

    if (script.tweenPlayAutomatic) {
    global.tweenManager.restartAutoTweens();
    }
    }
    }
    }

    and then call that function when you want the tween and behavior to reset. 

    You can see an example of this in use in the SpectaclesTimeLoopHelper script of the Spectacles Depth/Path template. 

    Secondly, one thing to be careful about is that the Behavior script does not manage the instantiated object--this means that over time your game will keep creating new objects which might overload the phone's memory.

    Consider taking a look at the Spawner script found in the High Score template, which has an option to destroy spawned objects after certain time. Another way to do it maybe is to create a script in the prefab which calls destroy() on itself after a certain period of time

    var delayedEvent = script.createEvent("DelayedCallbackEvent");
    delayedEvent.bind(function(eventData)
    {
    print("delay is over");
    script.getSceneObject().destroy();
    });

    // Start with a 2 second delay
    delayedEvent.reset(2);
    print("delay has started");

     

    Let me know if you need any clarification!

    Thanks,

    Jonathan 

    Comment actions Permalink
  • HI Jonathan,

     

    Thanks for responding in such difficult times. Much appreciated.  Thank you for the 'destroy' function - that helps heaps!

    On the tween restart I'm struggling to get my head around it.

    I can see that this will reset the tweens, but what I really need is to reset the counter/countdown in some way. In the game, the counter is counting down from 20 to 0. When it hits zero the game resets itself. I was hoping there was a way to reset the counter when the camera flips which would in effect reset the game - a forced countdown to zero if you like

    See I need to stop the behaviours that have been initialized in whack a mole game, reset the counter and bring up the initial start screen. - basically reset the whole thing to its original state.

    Does that make sense?

    As said I'd not a coder, so I get a bit fuzzy when I see the code - where to apply it etc. so if you could walk me through that would be of immense help!

     

    Thanks again for your response :)

    Comment actions Permalink
  • Hi There,

     

    I've tried to implement the above scripting to no avail. Could you help me little more?

    So I added a reset tweens script in the objects panel and a behaviour. I'm not sure this is the right way to call it?

     

    Here's the script you created:

    Then a behaviour script which enables/disables the script. 

     

    My understanding (which may be incorrect) is that when I flip to front lens, this should enable the script and reset all the tweens and behaviour. When switches back to back lens it should disable and so on through flipping.

    Your thoughts on this would be appreciated. I can't seem to get to work!

     

    Best,

    Rich

    Comment actions Permalink
  • Hi there, Any thoughts on this? Cheers.

    Comment actions Permalink
  • Hi Richard,

    You're close. We just need to modify that script to let it listen to a custom trigger on Behavior:

    global.behaviorSystem.addCustomTriggerResponse("triggerReset", onLoop)

    Then in your Behavior Script call the custom trigger "triggerReset"

    Additionally, for optimization, you can comment out the original script that has to do with resetting base on time (since we're manually controlling it. 

    // SpectaclesTimeLoopHelper.js

    // Version: 0.0.1

    // Event: Lens Initialized

    // Description: This script manages what helper scripts should do when time loops in Lenses for Spectacles.




    //@ui {"widget":"label", "label":"On Spectacles Time Loop"}

    //@ui {"widget":"separator"}

    //@ui {"widget":"group_start", "label":"Tween Manager"}

    //@input bool tweenReset = true {"label":"Reset Tweens"}

    //@input bool tweenPlayAutomatic = true {"label":"Play Automatic", "showIf": "tweenReset"}

    //@ui {"widget":"group_end"}

    //@ui {"widget":"separator"}

    //@ui {"widget":"group_start", "label":"Behavior"}

    //@input bool behaviorReinit = true {"label":"Reinitialize"}

    //@input bool behaviorCallTurnOn = true {"label":"Trigger Turn On Event"}

    //@ui {"widget":"group_end"}




    //var prevTime = getTime();




    //function checkTime() {

    // var curTime = getTime();

    // if (prevTime > curTime) {

    // onLoop();

    // }

    // prevTime = curTime;

    //}

    //




    global.behaviorSystem.addCustomTriggerResponse("triggerReset", onLoop)




    function onLoop() {

    if (global.behaviorSystem) {

    if (script. behaviorReinit) {

    global.behaviorSystem.sendCustomTrigger("_reinitialize_all_behaviors");

    }

    if (script.behaviorCallTurnOn) {

    global.behaviorSystem.sendCustomTrigger("_trigger_all_turn_on_behaviors");

    }

    }




    if (global.tweenManager) {

    if (script.tweenReset) {

    global.tweenManager.resetTweens();




    if (script.tweenPlayAutomatic) {

    global.tweenManager.restartAutoTweens();

    }

    }

    }

    }

    //

    //script.createEvent("UpdateEvent").bind(checkTime);

     

    Cheers!

    Jonathan 

     

    Comment actions Permalink
  • Hi Richard,

    You're close. We just need to modify that script to let it listen to a custom trigger on Behavior:

    global.behaviorSystem.addCustomTriggerResponse("triggerReset", onLoop)

    Then in your Behavior Script call the custom trigger "triggerReset"

    Additionally, for optimization, you can comment out the original script that has to do with resetting base on time (since we're manually controlling it. 

    // SpectaclesTimeLoopHelper.js

    // Version: 0.0.1

    // Event: Lens Initialized

    // Description: This script manages what helper scripts should do when time loops in Lenses for Spectacles.




    //@ui {"widget":"label", "label":"On Spectacles Time Loop"}

    //@ui {"widget":"separator"}

    //@ui {"widget":"group_start", "label":"Tween Manager"}

    //@input bool tweenReset = true {"label":"Reset Tweens"}

    //@input bool tweenPlayAutomatic = true {"label":"Play Automatic", "showIf": "tweenReset"}

    //@ui {"widget":"group_end"}

    //@ui {"widget":"separator"}

    //@ui {"widget":"group_start", "label":"Behavior"}

    //@input bool behaviorReinit = true {"label":"Reinitialize"}

    //@input bool behaviorCallTurnOn = true {"label":"Trigger Turn On Event"}

    //@ui {"widget":"group_end"}




    //var prevTime = getTime();




    //function checkTime() {

    // var curTime = getTime();

    // if (prevTime > curTime) {

    // onLoop();

    // }

    // prevTime = curTime;

    //}

    //




    global.behaviorSystem.addCustomTriggerResponse("triggerReset", onLoop)




    function onLoop() {

    if (global.behaviorSystem) {

    if (script. behaviorReinit) {

    global.behaviorSystem.sendCustomTrigger("_reinitialize_all_behaviors");

    }

    if (script.behaviorCallTurnOn) {

    global.behaviorSystem.sendCustomTrigger("_trigger_all_turn_on_behaviors");

    }

    }




    if (global.tweenManager) {

    if (script.tweenReset) {

    global.tweenManager.resetTweens();




    if (script.tweenPlayAutomatic) {

    global.tweenManager.restartAutoTweens();

    }

    }

    }

    }

    //

    //script.createEvent("UpdateEvent").bind(checkTime);

     

    Cheers!

    Jonathan 

     

    Comment actions Permalink
  • Hey Jonathan,

    Thanks for getting back. Still struggling with it. This is my set up:

     

    I've replaced the script with the new one supplied which is now titled triggerReset. The behaviour triggers on front camera and the Trigger Name is triggerReset.

    Nothing is happening. Is the set up correct? Just to be sure, this will reset all tweens and behaviours back to their original states?

    Is it worth sending you the files or am I missing something simple here?

    Comment actions Permalink
  • Hi Richard,

    Hmm that's strange--what version is your Behavior Script, maybe it doesn't have the reset API. If you open the script it should be

    // Version: 0.0.4

    In the onLoop function, try adding a bunch of print("1") inside the function and if statements, that way you can see whether the script or the pieces of the script are being called or not.

    Thanks!

    Jonathan 

    Comment actions Permalink
  • Hi Jonathan,

    It's version 0.0.4

    I placed the print function as follows and can't see that.

     

    Not sure in the right place! 

    Comment actions Permalink
  • Hi Jonathan,

     

    I've put the print function here and it prints:

     

    So that works but still not performing when on mobile device.

    Comment actions Permalink
  • Hi Richard,

    Hmm, can you try this example project where I combined the trigger and behavior? 

    Thank you,

    Jonathan

    Comment actions Permalink
  • Hi Jonathan,

     

    I've tried the example project and this is the logger for it. I dropped it into my project as well. This still doesn't seem to work.

    Comment actions Permalink
  • Hi Richard,

    Hmm, it should reset your behavior and tween. Can you share the project for us to take a look at? Might be easier than just going back and forth :)

    Thanks,

    Jonathan 

    Comment actions Permalink
  • Yes that would be great. Do you have an email I can send to?

    Comment actions Permalink
  • Hi Jonathan,

    I'm good to send you the files if you're ok with that?

    Best,

    Rich

    Comment actions Permalink
  • Hey Jonathan,

    Any chance of sending this file over to you?

    Best,

    Rich

    Comment actions Permalink
  • Hi Richard,

     

    We have spent some time looking over your project. It is such an amazing project and quite complex! We have crafted and sent you a custom response for the specific problem on camera switch. Enjoy!

     

    Happy creating :)

     

    Nico

    Comment actions Permalink
  • There is no inbuilt method to reset a Lens. You can however write a script that sets the values of your Lens to a starting state.

    Comment actions Permalink

We're here to help! We just need a little bit of information...

What system are you using?

Have you downloaded the latest version of Lens Studio?

Please download the latest version of Lens Studio. If you still run into this issue, please come back and report it!

Is this issue causing Lens Studio to crash?

What system do you run Lens Studio on?

Version

Graphics

Follow the next steps to help us solve your issue:

  • Copy and paste this text into your TerminalCommand Window
    open ~/Library/Preferences/Snap/Lens\ Studio/ %LOCALAPPDATA%\Snap\Lens Studio Copy Text
  • Press ReturnEnter to run the command. The Lens Studio folder will automatically open
  • Prepare to upload your files: zip the "Log" Folder by right-clicking and choosing "compress."
    Locate the Log.txt file right above it.

    Attach a screenshot of the issue:

Name:

Email:

What is this most relevant to?

Please enter a brief description of your issue:

Thanks for submitting this issue.

Unfortunately, it's likely due to the operating system or hardware you're using – since they don't meet the system requirements for Lens Studio.

Still, we hear you loud and clear, and are logging the issue in case there's ever a workaround we can provide!

Minimum Requirements

Operating System: Windows 10 (64 bit); MacOS 10.11+

Hardware: Minimum of Intel Core i3 2.5Ghz or AMD Phenom II 2.6Ghz with 4 GB RAM; Intel HD Graphics 4000 / Nvidia GeForce 710 / AMD Radeon HD 6450 or better; screen resolution of 1280x768 or higher

We'll try to resolve this issue as soon as possible. Thanks for letting us know about it!

Keep an eye out for a followup email from us. We may have a couple more questions for you, or we might already have a solution to offer.

Happy creating!