Problems with ConfigFile and Time.get_unix_time_from_system()

Godot Version

v4.2.1.stable.official

Question

Hello there. I’m so sorry for asking, probably obvious thing, but I am trying to learn GDScript by myself and have some troubles with saving through config file.
So the thing I am trying to do is:

  1. Save the time when application was closed as LastLaunchTime using Time.get_unix_time_from_system()
  2. Load it when application starts on _ready()
  3. To than calculate the difference with current unix time and tell how much seconds passed since application was closed.

The problem is, that when I try to do it in _ready() my lastLaunchTime always equal to the same whole number, no matter what I do. Please, tell me where is my mistake.

extends Node2D

var config = ConfigFile.new()
var dayCount = 1
var lastLaunchTime = 0
var DoT_Stamina = 0.5
var DoT_Hunger = 0.5
var DoT_Psyco = 0.5

func save_data():
	config.set_value("Player", "Health", $/root/Game/UI/Bar_State/Bar_Health.value)
	config.set_value("Player", "Stamina", $/root/Game/UI/Bar_State/Bar_Stamina.value)
	config.set_value("Player", "Hunger", $/root/Game/UI/Bar_State/Bar_Hunger.value)
	config.set_value("Player", "Psyco", $/root/Game/UI/Bar_State/Bar_Psyco.value)
	config.set_value("Player", "DayCount", dayCount)

	config.save("user://save.cfg")

func _ready():
	var err = config.load("user://save.cfg")

	if err != OK:
		config.set_value("Player", "Health", 100)
		config.set_value("Player", "Stamina", 100)
		config.set_value("Player", "Hunger", 100)
		config.set_value("Player", "Psyco", 100)
		config.set_value("Player", "DayCount", 1)
		config.set_value("Player", "LastLaunchTime", 0)
	else:
		$/root/Game/UI/Bar_State/Bar_Health.value = config.get_value("Player", "Health")
		$/root/Game/UI/Bar_State/Bar_Stamina.value = config.get_value("Player", "Stamina")
		$/root/Game/UI/Bar_State/Bar_Hunger.value = config.get_value("Player", "Hunger")
		$/root/Game/UI/Bar_State/Bar_Psyco.value = config.get_value("Player", "Psyco")
		$/root/Game/UI/DayCounter.text = "Day " + str(config.get_value("Player", "DayCount"))
		lastLaunchTime = config.get_value("Player", "LastLaunchTime")
		print(lastLaunchTime)
		var currentTime = Time.get_unix_time_from_system()
		print(currentTime)
		var elapsedSeconds = currentTime - lastLaunchTime
		print(elapsedSeconds)
		$/root/Game/UI/Bar_State/Bar_Stamina.value -= DoT_Stamina * elapsedSeconds
		$/root/Game/UI/Bar_State/Bar_Hunger.value -= DoT_Hunger * elapsedSeconds
		$/root/Game/UI/Bar_State/Bar_Psyco.value -= DoT_Psyco * elapsedSeconds
	# Збереження даних
	save_data()    
	
func _exit_tree():
	var currentTime = Time.get_unix_time_from_system()
	config.set_value("Player", "LastLaunchTime", currentTime)
	save_data()

If you open up your config file, you can see that it’s stored like this

LastLaunchTime=1.70796e+09

For some reason, config file uses scientific notation when storing floats. You can avoid this by turning the float into an int like so:

	var currentTime = int(Time.get_unix_time_from_system())

You saved me, thank you a lot. I spent a few days trying to understand what is going wrong. Bless you!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.