I did a test and the following functions work.
Saving:
var file = File.new();
file.open("user://TestFile.bob", File.WRITE);
file.store_var(0.753);
var new_node = Label.new();
new_node.text = "Saved label!"
file.store_var(new_node);
file.close();
Loading:
var file = File.new();
file.open("user://TestFile.bob", File.READ);
var expected_float = file.get_var();
if typeof(expected_float) == typeof(0.5):
text = str(expected_float / 0.5);
var new_node = file.get_var();
if new_node is Label:
add_child(new_node);
new_node.rect_position = Vector2(rand_range(-100, 100), rand_range(-100, 100))
file.close();
The reason it returns Variant
is because of how Godot uses variants for cross platform variable storing.
As this page from the docs says, variants take 20 bytes and they are generally used temporarily. I would be willing to wager (not seriously though) that most of the data types in Godot have Variant
as their base class.
This makes it where programmatically almost any type can be a variant, since it is the back bone of practically everything in Godot. This gives Godot a extremely low level polymorphic way to handle data types. If you are used to static type languages, you may be aware that you have to define what data type your functions return, and what data types the arguments passed in have to be.
Because Godot uses a variant, it can take any class that extends a variant! This make it extremely flexible since you can make it where you can have functions that take all sorts of different data types as their arguments. The problem becomes figuring out what you were passed, since a Variant
could be practically anything. By doing a some type comparison checks, we can narrow down what was passed, and then either cast it to the type we want (for a static language) or go straight to using it like it is the type it is (for a dynamic language).
Once we know what the data type is, we can use the specific features that the data type places on top of the Variant
class since whatever we want wanting to use extends Variant
(We probably have to cast it into the data type we want in static languages, which is slightly different but the gist is the same).
Of course, this is mostly speculation and educated guesses from my experience programming. It is quite possible that it works differently and I do not know what I'm talking about :sweat_smile: