Why does game data load as floats even though it's intergers?

Godot Version

4.4

Question

So my save file is this: {“unlocked_levels”:[1,2]}

And I DO know that that is the same file it’s loading, I’ve checked.

So my load code is this:

	var save_game = FileAccess.open("user://savegame.dat", FileAccess.READ)
	
	while save_game.get_position() < save_game.get_length():
		var json_string = save_game.get_line()
		var json = JSON.new()
		var parse_result = json.parse(json_string)
		var node_data = json.get_data()
		unlocked_levels = node_data.unlocked_levels
		print(node_data.unlocked_levels)

(I used Gwizz’s tutorial)

But when I have it print, it prints: [1.0, 2.0] Even though in the save file it’s: [1,2]

1 Like

JSON numbers are parsed as floats always. It’s listed in the documentation JSON — Godot Engine (stable) documentation in English

Numbers are parsed using String.to_float() which is generally more lax than the JSON specification.

So how do I convert the floats to integers in the array? Cause I know int() exists, but that is for one thing, not for a whole array.

You could use Array.map() like:

var int_array = my_array.map(func(v): return int(v))
1 Like

Thank you so much!