Why can't I get my _process(delta) function to calculate time faster?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By Robster
:warning: Old Version Published before Godot 3 was released.

Hi all,

I have the following code:

var time_mult = 10000.0		#how fast time goes by
var second = 0
var minute = 0
var hour = 0
var day = 1
var month = 1
var year = 1

var paused = false

and

func _process(delta):
	
	#work out the clock/time/date logic
	if not paused:
		
		second += delta * time_mult
		
		if second >=60:
			second = 0
			minute += 1
		if minute >= 60:
			minute = 0
			hour += 1
			print("hour passed")
		if hour >= 24:
			hour = 0
			day += 1
			print("day passed")
			get_node("/root/gameLevel/InfoBar/LabelDay").set_text(str(day))
		if day >= 28:
			day = 0
			month += 1
			get_node("/root/gameLevel/InfoBar/LabelMonth").set_text(str(month))
		if month >= 12:
			month = 0
			year += 1
			get_node("/root/gameLevel/InfoBar/LabelYear").set_text(str(year))

So I can change the var time_mult and it speeds up the clock. Dramatically. But I can’t get it to speed up enough. I can get it up to about where it is now (10000 times faster or there abouts) and the print statement for hour passed only happens once a second.

I can lift the time_mult to 100000000 and it never goes faster than once a second.

What am I doing incorrectly here? Why is it maxing out at that multiplier value?

Thanks so much…

:bust_in_silhouette: Reply From: jandrewlong

When your _process has more than one second to process, it discards the extra.

Try changing:

if second >=60:
   second = 0
   minute += 1
if minute >= 60:
    minute = 0
    hour += 1
...

to

while second >= 60:
    second -= 60
    minute += 1
while minute >= 60:
    minute -= 60
    hour += 1
...

Thanks for your reply. I changed it as suggested but the behavior is identical.

EDIT: My mistake. I didn’t even see the while loop.

Robster | 2017-08-16 12:08

:bust_in_silhouette: Reply From: quijipixel

The problem is here:

if second >=60:
            second = 0
            minute += 1

It doesn’t matter how big the multiplier is, it will always add just 1 minute every frame. Try this instead:

second += delta * time_mult
minute = int(second / 60)
hour = int(second / 3600)

jandrewlong works, but I think avoiding so many while loops per frame when you can just calculate how much time has passed is a better approach.

OK now that makes sense and ALMOST works really well.

Here’s what’s happening:

If I set var time_mult = 50000.0 then it kicks off and starts counting about 1 second after I run the game. The clock flies by. Very cool (but way too fast).

If I set var time_mult = 10000.0 it takes about EIGHT seconds before the clock starts ticking by. It just sits there doing nothing until about the 8 second mark.

If I set var time_mult = 5000.0 it takes about SIXTEEN seconds before the clock starts.

What would be causing the large delay before it starts? I can’t use it obviously like that and in reality I’ll need to drop that number down dramatically lower than 5000 to make it work.

Thanks so much, I appreciate your help and advice…

Robster | 2017-08-16 12:17

I noticed you only show the days, months and years that have passed. It makes sense it takes 16 seconds, I tried the code using a multiplier of 1000 and added the hours to the screen, and it takes a while to reach the first day but the clock is ticking, it just takes a while to reach the amount of hours to show a change in days.
I modified the code a little, maybe this works for you:

var time_mult = 100000.0     #how fast time goes by
var second = 0
var minute = 0
var hour = 0
var day = 1
var month = 1
var year = 1

var paused = false


func _process(delta):

	#work out the clock/time/date logic
	if not paused:
		second = delta * time_mult
		minute += second / 60
		hour += second / 3600
		get_node("Label").set_text(str(hour))

		if hour >= 24:
			hour = 0
			day += 1
			print("day passed")
		get_node("day").set_text(str(day))
		
		if day >= 28:
			day = 0
			month += 1
		get_node("month").set_text(str(month))
		
		if month >= 12:
			month = 0
			year += 1
		get_node("year").set_text(str(year))

I did my test with local nodes, just change those to your labels on root node

quijipixel | 2017-08-16 12:45