Guest User

Coutdown.gd

a guest
Feb 19th, 2024
58
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. extends Panel
  2.  
  3. ##TO DO
  4. ##Create a script that can correctly convert the countdown timer from seconds
  5. ##into minutes, seconds, and miliseconds.
  6. ##There doesn't appear to be a property for the timer class for this in the
  7. ##Documentation so we will need to do this ourselves.
  8.  
  9. ##Once we have the time remaining converted properly we can then get to work
  10. ##Displaying it on the UI.
  11.  
  12. #using an export varible to set Timer.wait_time so I don't need to go between
  13. #tabs so much.
  14. static var timerWaitTime : int = 120
  15.  
  16. #storing timer as a var incase we need get and set functions later down the line
  17. @onready var countdownTimer := $countdownTimer
  18.  
  19. #first approach we're going to do will involve storing minutes, seconds and
  20. #milliseconds as seperate varibles which we will then set as label text.
  21. var timerMinutes : int
  22. var timerSeconds : int
  23. var timerMilliseconds : int
  24.  
  25. func _ready():
  26.     countdownTimer.wait_time = timerWaitTime
  27.     countdownTimer.start()
  28.  
  29.  
  30. func _process(delta):
  31.     #take wait_time property and truncate the time so we are only left with whole seconds.
  32.     #We can deal with the fractions of a seconds later.
  33.    
  34.     timerSeconds = int(countdownTimer.time_left)
  35.     #print("Seconds remaining: ", timerSeconds)
  36.    
  37.     timerMinutes = timerSeconds / 60
  38.     #print(timerMinutes)
  39.    
  40.     #Subtract the remaining minutes so we're only left with the seconds
  41.     for minutes in timerMinutes:
  42.         timerSeconds -= 60
  43.    
  44.     var timerOutput : String
  45.    
  46.     #cant think of a way to run this through a switch case in a way that isnt
  47.     #stupid so I'm using if else
  48.     if timerSeconds <= 9:
  49.         %TimerLabel.text = str(timerMinutes, " : 0", timerSeconds)
  50.    
  51.     else:
  52.         %TimerLabel.text = str(timerMinutes, " : ", timerSeconds)
  53.    
  54.  
Advertisement
Add Comment
Please, Sign In to add comment