In this blog we are going to show how to create a simple count down timer. The timer will start when the user clicks the box in VR, and once the countdown is done an alarm sound will beep.
- Start by creating an empty scene in SimLab VR Studio, then from the Libraries at the bottom, select 3D scenes/Environment then Ground. This download will require sing in/sing up.
- From the Create menu, Shapes select Box and create one with the default dimensions. Create Variable Writer from the VR Effects menu.
- Open Training Builder and click Edit Variables button at the top right corner of its dialog. In the Edit Variables and Initial Values dialog, create two variables:
Time variable (Number), which is the count down time in seconds. For testing we’ll set this variable to 10s.
Set_timer variable (String), which is going to be used as a trigger for the timer. Set its initial value to ‘false’.
- Back to the 3D area, select the VR_Variable_Writer and in its properties dialog to the right, link it to the variable Time. Add ‘Time in sec’ Prefix.
- Click Ctrl+Enter to view the scene in VR Viewer, displaying starting time with no functions added yet.
- Back to the Training Builder dialog, add Node Triggered Event and select box geometry as SceneNode. When the user clicks on the box, the countdown should start so we add Change Variable Value Response. When the user triggers the box the value of the Set_timer variable is set to ‘true’
- Equal to the ‘if clause’ in programming we want the countdown to start when the Set_timer is ‘true’ and the Time variable is greater than zero.
To do this we add two Monitor Variable Value Boolean operations, one with Set_timer variable, and check if its value is ‘true’. The other is for Time variable to make sure its value is still greater than zero.
Combine the two in an And Operation as shown below:
- Each time both of these two conditions are true, we want to pause for one second by adding Delay component, then decrement Time variable by one second in Change Variable Value component.
- The Set_timer variable is still True, to trigger the loop we need to change its value to False then to True again. This can be done by adding two consecutive Change Variable Value components.
- When the value of Time variable is equal to zero an alarm will ring. This can be done by adding Monitor Variable Value component, and Sound Action response.