Forum Discussion

Jonathan_Hill's avatar
Jonathan_Hill
Super Hero
10 months ago

FREE: Perpetual Dial

Spinning out of my work on E-Learning Challenge #453, here's my early entry for Challenge #454, Using Variables.

Try it for yourself here.

This demo uses Number, Text and True/False variables to trigger events based on the position of the dial.

But as you can see, this dial spins in both directions - seemingly forever* - overcoming one of the limitations of this interactive object.

I've achieved this by incorporating the following Javascript, which reports the position of the dial as 0-359° despite the dial having a rotation of 0-9360°.

// Function to calculate the position within the range of 0-359
function calculatePosition(dial) {
    // Calculate the position within the range of 0-359
    var position = (dial % 360 + 360) % 360;

    return position;
}

// Get the value of the Storyline variable Dial and calculate Position
var Dial = player.GetVar("Dial"); // Assuming "Dial" is the name of your Storyline variable
var Position = calculatePosition(Dial);

// Set the Storyline variable "Position" to the adjusted value
player.SetVar("Position", Position); // Assuming "Position" is the name of your Storyline variable


Download the file to see how it works, and please let me know if you have any questions.

* Try spinning it continuously in one direction - it'll take you a while to reach the end, but see what happens 😃

  • Really nice Jonathan, one question and probably because I don't use dials very much, could you not get perpetual potion by setting to 360 and then add a trigger to change value of the variable to 0 when it is 359 or is that bad practice

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Thanks!  That was one of the first things I tried, Phil.  But 'overwriting' the underlying variable when it reaches the start or the end position doesn't seem to have the effect of resetting the dial's position, oddly.  I also tried reloading the slide to achieve this, but no luck.

  • and I answered my own question it doesn't work, and that must be a bug

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Interestingly, it's a similar story with sliders, Phil.  A trigger to reset the slider variable when the slider reaches the end only changes the variable, not the slider's position.

      Yet a when user clicks trigger - activated before the slider reaches the end - does reset both the underlying variable and the slider's position

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Closest I can get to it is this - a trigger timed to the entry animation on a hidden object, which becomes visible when the slider is ≥ 99 



      It's a bit jerky for my liking.

      Similar visual issues when I use the same method on a dial, and it also takes a moment for the change to take effect and you have to grab the dial again.

  • Hi Jonathan. Thanks for sharing this really nifty solution. Also, I know it's just a small detail, but the little dial-turning sound effect you used makes the whole thing feel extra realistic.

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Thanks Bianca. All of the sound effects are CC0 from Freesound.org.  I'm a heavy user and regularly donate to keep the platform going.

      Pretty much any sound you can think of, want, or need is there.

  • TerryThomas's avatar
    TerryThomas
    Community Member

    Hey Jonathan, very creative with the modular division!  I haven't thought deeply about this question, i'll admit... but why does the code perform that mod division twice?  What situation(s) comes up where the first one does not give you the answer you need, of 0-359 degress?  (i can't open the story file at work, to see any comments or workings; long-story-short, we can't upgrade SL360 at the moment). thanx!

    • Jonathan_Hill's avatar
      Jonathan_Hill
      Super Hero

      Thanks. I wrote this code with the help of Claude.ai, and looking back at my notes I can see I queried that at the time. 'The double modulo ensures that a number wraps around correctly within a specific range, handling both positive and negative inputs.'