Author Topic: Writing a Variable Countdown Timer  (Read 2250 times)

Khalarin

  • Newbie
  • *
  • Posts: 13
Writing a Variable Countdown Timer
« on: July 30, 2020, 08:54:34 AM »
Hi guys,

So I'm looking to create a variable length timer based on Voice Input "set countdown timer for xx mins" the max length of timer needed is up to 40 mins, and have it calculate at what time that in game event will end & display reminders every 5 mins on the remaining time on the countdown.

This is for Elite Dangerous VA with EDDI, for tracking High Grade Emissions instances of up to 40 mins

I've seen the ships bell you did a while back and wondered if that could be converted for this use?

Thanks,

- Khal

Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Writing a Variable Countdown Timer
« Reply #1 on: July 30, 2020, 02:05:10 PM »
Set countdown timer for [2..40] minutes;Set countdown timer for 1 minute
Code: [Select]
Begin Text Compare : [{CMDALREADYEXECUTING}] Equals '1'
    Write [Red] 'High Grade Emissions timer is already running' to log
End Condition
Set date [>highGradeEmissionsTimerEnd] value to [Not Set]
Set date [>highGradeEmissionsTimerEnd] to [>highGradeEmissionsTimerEnd] plus [{TXTNUM:"{CMD}"}] minutes
Start Loop While : [>highGradeEmissionsTimerEnd] Is After Current Date/Time
    Set integer [~minutesRemaining] value to the converted value of {EXPDECINV: ({DATETICKS:>highGradeEmissionsTimerEnd} - {DATETICKS}) / 600000000}
    Begin Integer Compare : [~minutesRemaining] Is Greater Than Or Equals 5
        Write [Gray] 'High Grade Emissions timer expires in {INT:~minutesRemaining} minutes (at {TIMEHOUR24:>highGradeEmissionsTimerEnd}:{TIMEMINUTE:>highGradeEmissionsTimerEnd})' to log
        Pause 300 seconds
    Else
        Write [Gray] 'High Grade Emissions timer expires in less than five minutes (at {TIMEHOUR24:>highGradeEmissionsTimerEnd}:{TIMEMINUTE:>highGradeEmissionsTimerEnd})' to log
        Set decimal [~secondsRemaining] value to the converted value of {EXPDECINV: ({DATETICKS:>highGradeEmissionsTimerEnd} - {DATETICKS}) / 10000000}
        Pause a variable number of seconds [~secondsRemaining]
        Write [Pink] 'High Grade Emissions timer has expired' to log
        Set date [>highGradeEmissionsTimerEnd] value to [Not Set]
    End Condition - Exit when condition not met
End Loop

Make sure to check the "Evaluate Not Set as current date/time" option in the "Set a Date/Time Value" action

You could use the "Do not execute this command if it is already running" option instead of the first three actions, if you prefer.

The actual timer variable is profile-scoped rather than command scoped, specifically so you can use another command to check the remaining time manually, if you want:
Check high grade emissions timer
Code: [Select]
Begin Text Compare : [{CMDACTIVE:Set countdown timer for 2 minutes}] Equals '1'
    Set integer [~minutesRemaining] value to the converted value of {EXPDECINV: ({DATETICKS:>highGradeEmissionsTimerEnd} - {DATETICKS}) / 600000000}
    Write [Gray] 'High Grade Emissions timer expires in {INT:~minutesRemaining} minutes (at {TIMEHOUR24:>highGradeEmissionsTimerEnd}:{TIMEMINUTE:>highGradeEmissionsTimerEnd})' to log
Else
    Write [Red] 'High Grade Emissions timer is not running' to log
End Condition
« Last Edit: July 31, 2020, 01:27:28 PM by Pfeil »

Khalarin

  • Newbie
  • *
  • Posts: 13
Re: Writing a Variable Countdown Timer
« Reply #2 on: July 31, 2020, 04:24:38 AM »
Morning Pfeil, thank you for that I'm going to have a good look through it, browsing the forums I found a really nice looking timer I've been adapting had a few weird things happen so I obviously don't understand something going on within the code:

Quote
Set Countdown for [1..40] Minutes
Code: [Select]
Set small int (condition) [Timer] value to 0
Set small int (condition) [UpdateLog] value to 0
Set small int (condition) [MinsElapsed] value to 0
Begin Text Compare : [{EXP: {TXTNUM:"{CMD}"} > 0}] Equals '1'
    Set small int (condition) [Timer] value to the converted value of {EXP:({SMALL:Timer} + {TXTNUM:"{CMD}"})*60} (save value to profile)
    Write [Purple] ':/> Timer set for [{SMALL:Timer}] Seconds' to log
End Condition
Set small int (condition) [TimerCount] value to the value of [Timer]
Set small int (condition) [MinsRemaining] value to the value of [{EXP:({SMALL:TimerCount} / 60)}]
Start Loop While : [TimerCount] Does Not Equal 0
    Pause 0.1 seconds
    Set small int (condition) [TimerCount] value as decremented by 1
    Set small int (condition) [UpdateLog] value as incremented by 1
    Begin Small Integer Compare : [UpdateLog] Equals 60
        Set small int (condition) [MinsElapsed] value as incremented by 1
        Write [Purple] ':/> [{SMALL:MinsElapsed}] Minute Has Elapsed' to log
        Write [Purple] ':/> [{SMALL:MinsRemaining}] Remaining' to log
        Begin Small Integer Compare : [MinsElapsed] Is Greater Than Or Equals 2
            Write [Purple] ':/> [{SMALL:MinsElapsed}] Minutes Have Elapsed' to log
        End Condition
        Set small int (condition) [UpdateLog] value to 0
    End Condition
End Loop
Write [Purple] ':/> Timer has stopped after [{SMALL:Timer}] Seconds' to log
Write [Purple] ':/> Timer has stopped after [{SMALL:MinsElapsed}] Minutes' to log
Say, 'Time is up Commander'


And then using to check time remaining:
Quote
How much time remains
Code: [Select]
Set small int (condition) [RemainingTime] value to the value of [TimerCount]
Write [Purple] ':/> Remaining Time is [{SMALL:RemainingTime}] Seconds' to log

That runs mostly fine, apart from the MinsRemaining I'm obviously not understanding how to use {EXP: properly so going to do some reading in the manual today along with that code you've done (thank you again) and see if i can shed some light on that.

Originally I was going to try and set it for absolute time, using both mins & secs but when I did create the cmd it said it was going to have to add 2360 dynamic commands which could slow VA down.

Khalarin

  • Newbie
  • *
  • Posts: 13
Re: Writing a Variable Countdown Timer
« Reply #3 on: July 31, 2020, 06:08:39 AM »
Pfeil, that code works really well. I did have to change a couple of little things the double ">>" stopped Check Timer from working it was giving:
Integer token could not be converted : {EXPDECINV: ({DATETICKS:>>highGradeEmissionsTimerEnd} - {DATETICKS}) / 600000000}
but once that was stripped out it worked perfectly =D

Code: [Select]
Begin Text Compare : [{CMDACTIVE:Set timer for 2 minutes}] Equals '1'
    Set integer [~minutesRemaining] value to the converted value of {EXPDECINV:({DATETICKS:>highGradeEmissionsTimerEnd} - {DATETICKS}) / 600000000}
    Write [Purple] ':/> HGE Timer expires in {INT:~minutesRemaining} minutes (at {TIMEHOUR24:>highGradeEmissionsTimerEnd}:{TIMEMINUTE:>highGradeEmissionsTimerEnd})' to log
Else
    Write [Red] ':/> HGE Timer is NOT running!' to log
End Condition


Pfeil

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 4782
  • RTFM
Re: Writing a Variable Countdown Timer
« Reply #4 on: July 31, 2020, 01:26:35 PM »
Ah, yes. I changed the command over from persistently profile scoped to normal profile scoped (as there is no need for the former), but I didn't check the second command.