Fileacess "File.get_float(myFloat)" is not working

I´m using Godot FileAcess system to save and load data for my game, but I don´t know how do can I load a float value?

using the Myfloat= myfile .get_var(Myfloat) not works

Code:

### Godot Version 4.5

var FileSlot = "user://FileSlot.txt"
var Myfloat :float

func _ready() -> void:
	if FileAccess.file_exists(FileSlot):
		var myfile = FileAccess.open(FileSlot, FileAccess.WRITE)
		Myfloat= myfile .get_float(Myfloat)

:ERROR: (X)

 
Line 15:Too many arguments for "get_float()" call. Expected at most 0 but received 1.

Myfloat is a variable name that does not exist in the file. It’s just stored there. My recommendation is you take a look at this tutorial which shows you how to save data to a file. Saving games — Godot Engine (stable) documentation in English

Alternately, I made a plugin for saving and loading data. GitHub - dragonforge-dev/dragonforge-disk: An Autoload singleton to handle saving and loading of game data

  1. Copy the dragonforge_disk folder from the addons folder into your project’s addons folder.
  2. In your project go to Project → Project Settings…
  3. Select the Plugins tab.
  4. Check the On checkbox under Enabled for Dragonforge Disk (Save/Load) Component
  5. Press the Close button.
  6. In your code to save the info write:
Disk.save_setting(Myfloat, "MyfloatName")
  1. In your code to load the info write:
var Myfloat = Disk.load_setting("MyfloatName")
1 Like

You have two errors that jump at me:

  1. You want to read from a file, but you are opening it for writing instead of opening it for reading. Opening for writing will also effectively erase the contents of your file.
  2. get_float() does not take an argument. It reads the next four bytes in the file, and returns them as a float.
2 Likes


Using READ its the same

FileAcess.get_float() does not accept any arguent. FileAccess keeps an internal read cursor and FileAcess.get_float() will read the next float from the file and advance the cursor 4 bytes.

2 Likes

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