Problems with ConfigFile and Time.get_unix_time_from_system()

Godot Version



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 =
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)"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)
		$/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")
		var currentTime = Time.get_unix_time_from_system()
		var elapsedSeconds = currentTime - lastLaunchTime
		$/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
	# Збереження даних
func _exit_tree():
	var currentTime = Time.get_unix_time_from_system()
	config.set_value("Player", "LastLaunchTime", currentTime)

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


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.