# Changing time values in branching

By
Jeni Johnson

I am creating a branching scenario where each choice adds time to a clock. The final results will be decided by the total time added up throughout the choices. For example, if the user has less than 1 hour=choice A, more than 1 hour= choice B.

What is the best way to add up the time during the course, show the time on each choice, and then have the final result change by total time earned?

## 6 Replies

Do you want the actual time spent (physical / real time) or are you assigning a time value for the slides they end up visiting? (Shortest path takes 3 slides with a 5 minute value each versus a longer path that adds 10 minutes...)

Hey Owen! Here's an example of what I'd like to do.

User has two choices of activity; make lunch at home or call in order.

If they choose make lunch it would add 10 minutes to their total. If they choose call in order ir wouldn't add in any time.

The clock starts at 7am and the goal is to get to work by 8am in the game. All their choices add time to the clock.

Hope that makes sense!

Something like this? This example let's you add 3 different time increments to the total until you exceed 2 hours. If this is similar to what you are looking for, the SL360 .story file is in the resources of the published content. It does rely on a small amount of JavaScript because.... that's what I do. ðŸ˜‰

Link to Example

I would expect nothing less than Javascript from you. :)

Would you mind briefly explaining what the JS is doing?

I'll walk through all of the course logic. Essentially, you have 2 variables, one to track the elapsed time and one to display the "current" time.

Each button, when pressed, increases the elapsed time by the set amount assigned to that button. Each time the Elapsed time changes, it executes some JavaScript that updates the displayed time. Once 2 hours of elapsed time is met or exceeded, all of the buttons are disabled.

Now, here is the JavaScript logic for the display variable:

//This line is the call or connection to the SL player

var player=GetPlayer();//Now that the player is connected, this line gets the current value of the elapsed time variable and stores it in a JavaScript variable.

var totalTime = player.GetVar("ElapsedTime");//to determine the number of hours that have passed, we divide the elapsed time by 60 (minutes in an hour) and round down. Math.floor is the JS command to round down to the nearest whole number. So if the elapsed time was 59 minutes, then 59/60 = .9833 which rounds down to 0 which is exactly what we want since 59 minutes is not yet an hour.

var hours = Math.floor(totalTime/60);//Now that we know how many hours are represented in the elapsed time, we need to isolate the minutes that are unaccounted for. We do this by taking the total (or elapsed time) and subtracting from it the number of hours that have passed multiplied by 60. We do this so we are subtracting the hours in minutes from a total that is also stated in minutes. Using the same 59 minutes example as before, the math would look like 59-(0*60) which equals 59. If 65 minutes had elapsed, the code above would have yielded 1 hour and our math would look like this: 65-(1*60)=5 minutes.

var minutes = totalTime-(hours*60);//Now we need to format the minutes to always display it as 2 digits the way a digital clock does. In other words, if the number of minutes is less than 10, it will be a single digit that will need to have a "leading 0" placed before it. The following code checks for single digit minutes and adds the 0.

if (minutes<10){minutes = "0" + minutes;};//We also need to "move our clock forward" since we started at 7:00. So we simply add 7 to the number of hours that has passed.

var adjustedHours = hours + 7;//Now we put it all together the way we want it to show in the display variable and send it back to StoryLine. This code adds a leading 0 before the hour, followed by the hour, followed by the : symbol, followed by our 2 digit minute value. It stores it all in a single variable.

var displayTime = "0"+adjustedHours+":"+ minutes;//This code sends the display value back to the corresponding display value in StoryLine

player.SetVar("Display",displayTime);OOPS - I just realized you said "Brief"... LOLWow, this is amazing! Thanks so much for breaking it down in an easy to understand way. I'm excited to implement this in my project.