Attention | Topic was automatically imported from the old Question2Answer platform. | |
Asked By | Thallium |
So everything in this time system works aside from my AM → PM function. This happens within my advanceHour() function. For some reason after 12AM at night it switches to 1PM rather than 1AM.
My time system should be going from 6AM-12PM-2AM. I plan on sharing the complete project once finished so please feel free to make any recommendations Thank you!
var currentHour : int = 6
var currentMinute : int = 0
var currentPeriod : String = "AM"
var weekdays := ["Sunday", "Monday", "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"]
var currentDay : int = 0
@onready var timeLabel = $TimeLabel
var minuteTimer := Timer.new()
func _ready():
minuteTimer.wait_time = 60.0 / 60 # 7 seconds per 10 minutes // 8.5
minuteTimer.timeout.connect(_on_timer_timeout)
add_child(minuteTimer)
minuteTimer.start()
func _on_timer_timeout():
advanceTime()
updateUITime()
func advanceTime():
currentMinute += 10
if currentMinute >= 60:
currentMinute = 0
advanceHour()
func advanceHour():
var isAM : bool = true
var dayChange : bool = false
currentHour += 1
## MORNING 6AM -> 12PM ##
if currentHour >= 12:
if isAM == true && dayChange == false:
isAM = false
currentPeriod = "PM"
## MORNING 12PM -> 1PM ##
if currentHour >= 13:
if isAM == false && dayChange == false:
currentPeriod = "PM"
currentHour = 1
## NIGHT 1PM -> 12AM ##
if currentHour >= 12:
if isAM == false && dayChange == false:
currentPeriod = "AM"
dayChange = true
## NIGHT 12AM -> 1AM ##
if currentHour >= 13:
if isAM == true && dayChange == true:
currentPeriod = "AM"
currentHour = 1
## NIGHT 1AM -> 2AM ##
if currentHour == 2:
if isAM == true && dayChange == true:
currentPeriod = "AM"
dayChange = false
advanceDay()
func advanceDay():
currentHour = 6
currentMinute = 0
currentPeriod = "AM"
currentDay = (currentDay + 1) % 7
func updateUITime():
var timeString = str(currentHour).pad_zeros(2) + ":" + str(currentMinute).pad_zeros(2) + " " + currentPeriod
var weekdayString = weekdays[currentDay]
timeLabel.text = weekdayString + ", " + timeString
print_debug(weekdayString + ", " + timeString)
For some reason after 12AM at night it switches to 1PM rather than 1AM.
Have you stepped through that code a line at a time with the debugger? The issue should be easy to spot like that. However, by eye, it seems that “hour 13” will fall through a number of those if
checks in advanceHour()
- probably unintentionally (?).
I also find it odd that each call to advanceHour()
starts by assuming it’s currently AM
and there is no need for a day change. I’d expect that logic to be driven directly by the timekeeping system and not “reset” on each call…
While I know nothing of the Stardew Valley time keeping system, the above looks way more complicated than I assume is necessary…
jgodfrey | 2023-05-26 19:04
I would absolutely agree and would even say myself that I have butchered the AM, PM system. I will try going through with debugger again after taking a little break and seeing if I can solve the issue. If you have any recommendations as to how I could go about this in a more simple manor then that would be appreciated.
I had thought of something like this while I was away from the project, just not sure where or how I should implement it or if it even works lol. Gonna do some more testing now though and hopefully discover a solution!
var amPmString = if isAM: "AM" else: "PM"
var timeString = str(currentHour).pad_zero(2) + ":" + str(currentMinute).pad_zero(2) + " " + amPmString
Thallium | 2023-05-26 19:18
I’m not sure why you even need to track something like isAm
. Shouldn’t that be a given based on the value of your currentHour
?
jgodfrey | 2023-05-26 19:23
Yeah I don’t think I do, although I am still getting the same problem even after removing them. It never wants to switch back to AM once it hits midnight. Not sure how I should go about this. The bool was simply to try and avoid it but it did not work.
Thallium | 2023-05-26 19:57
I’ve whacked together some code that (I think) mostly satisfies the needs here (just for a different take on how to approach this). However, it needs some cleanup before it’s ready for public consumption. I’ll try to do that soon and post it here…
jgodfrey | 2023-05-26 20:53