How to Link a Character's Life to OS time?

(Using Godot Version 4)

Hello! I’m very new to Godot, and I’d like to have a game where a character’s needs are tied to the operating system’s time. For example, every 3 hours of real world time, the character needs to eat. When it’s 10pm operating system time, the character goes to sleep, etc. I understand this might be rather complicated, but any help is appreciated!

Operating System Testing

In a nutshell, this demo shows how you can get information from the operating system, or interact with the operating system.

The Time singleton is used to access system date and time.

1 Like

Thank you! I will download that and take a look through it. I also wonder if I couldn’t just get the date time and then add a timer that adds however many seconds from “birth” that the character needs to eat, and so on

It talks about timers:

	# Make a one-shot timer and wait for it to finish.
	await get_tree().create_timer(1.0).timeout

If I understand the question correctly.

To be honest I’m so new to gdscipt that I’m having trouble wrapping my head around it, but I’ll get there! I know there’s also a timer node, but it counts down to zero. I’d like a timer that counts up, and when it reaches a certain point, like the third day of “birth” there’s a 20% chance of character dying as it continues to count up.

Godot has very good documentation. You could start there.

It’s not that in a nutshell. The command is get_unix_time_from_system(). You have to convert that data into an integer. Mix that data with a Date. For example get_unix_time_from_datetime_string( String datetime ). Still convert it to integer. And have a way to compare the two. The timer is really easy. Just make a timer add to a Value every time. It’s easy. But, this time only works when the program is running. That’s why electronics have their own batteries.

That’s actually exactly what I did! Although it’s not good news to hear that it only works when the program is running because I’d like it to be keeping track of that when it’s closed. But I suppose when I get around to making it able to save, I can just save the number I’ve generated to indicate it’s age, check the current time converted to integer, and then add that to get the new age or something?

And how will the program work when it is closed? Well, you can run some agent in the background, but I don’t think many people will like it. And when the computer is turned off, such an agent also stops working. There is an option of i-net connection and work on time tracking on the server, but it requires a server and a lot of work.

Well I’m currently doing this to get the character’s “age” from spawn:

func _ready():
var birth = Time.get_unix_time_from_system()

func _process(delta):
var age = Time.get_unix_time_from_system() - birth #actual time - start time
print(age)

So I assume I can save the character’s age when closing the game, load it when the game is loaded, and then add to this script to see how much time has elapsed and add that on to the character’s age. And then from there see if enough time has passed that it should be hungry or something. Although I’m not there yet since I haven’t gotten to saving or loading data yet. I think that should bypass anything too complicated.

It’s not linked exactly to OS time like I thought it needed to be, but this solution works perfectly for my purposes thankfully!