Unexpected result when storing a float in disk using ResourceSaver
Asked Answered
T

2

0

I was trying to store in the save files the unix time in the system to later create a "Continue" button that load the latest save file calculated from each file unix time. But when I store the unix time float in disk using ResourceSaver it does some weird rounding:

This is a small replication project:

Button.gd

extends Button

func _on_pressed() -> void:
	var savefile :SaveFile = SaveFile.new()
	savefile.dict["unix_time"] = Time.get_unix_time_from_system()
	savefile.dict["float"] = 1708498594.489  # This is not the same value as the current unix time
	savefile.f = 1708498594.489                  # This is not the same value as the current unix time
	
	print(" -- savefile unix_time in memory: ",savefile.dict.unix_time)
	print(" -- savefile float in memory: ",savefile.dict.float)
	print(" -- savefile f in memory: ",savefile.f)
		
	await ResourceSaver.save(savefile,"res://savefile_dict.tres")
	
	savefile = load("res://savefile_dict.tres")
	print("\n -- savefile unix_time in disk: ",savefile.dict.unix_time)
	print(" -- savefile float in disk: ",savefile.dict.float)
	print(" -- savefile f in disk: ",savefile.f)

SaveFile.gd

extends Resource
class_name SaveFile

@export var dict :Dictionary
@export var f :float

It prints this:

I tried to use randf but it always defaults to 1708500000, maybe because randf returns a float.
Does anyone know why this happens? or how to solve it?

Tamekia answered 21/2, 2024 at 7:25 Comment(0)
S
1

I can reproduce this with your example. Looking at the resource written to disk you can see the precision has been removed. Potential bug?

[resource]
script = ExtResource("1_4jhr0")
dict = {
"float": 1.7085e+09,
"unix_time": 1.70855e+09
}
f = 1.7085e+09
Sha answered 21/2, 2024 at 21:13 Comment(0)
T
0

Sha I'll check if there's a github issue before posting one.

Edit

I worked around it casting the unix time float into an int.

There's multiple post talking about this issue:
https://github.com/godotengine/godot/issues/78204
https://github.com/godotengine/godot/issues/82424
https://github.com/godotengine/godot/issues/76573

Tamekia answered 21/2, 2024 at 23:9 Comment(0)

© 2022 - 2025 — McMap. All rights reserved.