something wrong with this time system, can any one help?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By huahuapro

i try this code:
https://forum.godotengine.org/42524/how-implement-a-virtual-date-and-time-system

the time is working well, but when i try to speedup the time, the event is missed.

i try to do add the time to an array, but nothing happened,

can anyone fix this?

thanks!

:bust_in_silhouette: Reply From: njamster

This should work:

extends ColorRect

signal time_passed(date_time)

var speed = 10.0

var prev_time = 0.0
var time = 0.0

class DateTime:
	var second
	var minute
	var hour
	var day

	func _init(time):
		var int_time = int(floor(time))

		second = int_time % 60
		minute = (int_time / 60) % 60
		hour = (int_time / (60 * 60)) % 24
		day = (int_time / (60 * 60 * 24))

	func equals(second, minute, hour, day):
		return self.second == second and self.minute == minute and self.hour == hour and self.day == day


func _ready():
	connect("time_passed", self, "_on_time_passed")


func _process(delta):
	time += delta * speed

	var date_times = []
	var int_time = floor(time)
	var prev_int_time = floor(prev_time)
	while prev_int_time < int_time:
		var new = DateTime.new(prev_int_time)
		date_times.append(new)
		prev_int_time += 1

	emit_signal("time_passed", date_times)

	prev_time = time


func _on_time_passed(date_times):
	for dt in date_times:
		if dt.equals(10, 0, 0, 0):
			color = Color("#FF0000")

I attached this script to a ColorRect-node and change its color to red once the event (10 in-game seconds passed) is reached. As speed is set to 10.0, this will equal the passing of one real-time second.

you fix it! you save my life sir! thank you.

huahuapro | 2020-03-26 00:15

I don’t know what’s going on here. How can I set the text of a label to the time without seconds since starting the game? Maybe with days:…, hours:…, seconds:…

MaaaxiKing | 2020-05-15 15:37

How can I set the text of a label to the time without seconds since starting the game?

I’m not sure what this has to do with this question or my answer to it. Why aren’t you posting this as a separate question? Anyhow… Here you go:

extends Label

var time_passed = 0.0

func _process(delta):
    time_passed += delta

    var int_time = int(floor(time_passed))

    var minutes = (int_time / 60) % 60
    var hours   = (int_time / (60 * 60)) % 24
    var days    = (int_time / (60 * 60 * 24))

    text = "days: %d, hours: %d, minutes: %d" % [days, hours, minutes]

njamster | 2020-05-15 17:29

Oh wow thanks a lot☺

MaaaxiKing | 2020-05-15 18:01

How can I do + 1 on time_passed independent of the frames, so every second?

MaaaxiKing | 2020-05-15 18:15

How can I do + 1 on time_passed independent of the frames, so every second?

I don’t think that’s possible. Even if you use a Timer-node, it works this way. Why would you want to do it independent of the frames in the first place?

njamster | 2020-05-15 19:46

I don’t want to do this, only asked out of interest.

MaaaxiKing | 2020-05-15 20:33

maybe do

yield(get_tree().create_timer(1), "timeout")
time_passed += 1

in _process() function?

MaaaxiKing | 2020-05-15 20:36

Oh, you said Timers work with delta too so probably it won’t work.

MaaaxiKing | 2020-05-15 20:40

A bit late! but this will work

extends Label

var time_passed = 0.0

func _process(delta):
	time_passed += delta

	var int_time = int(floor(time_passed))
	
	var secs = fmod(time_passed,60)
	var minutes = (int_time / 60) % 60
	var hours   = (int_time / (60 * 60)) % 24
	var days    = (int_time / (60 * 60 * 24))

	text = "days: %d, hours: %d, minutes: %d, secs: %d" % [days, hours, minutes, secs]

rungeon | 2021-01-15 19:26