Hi all. I'm going to use file.get_var and store_var to store input events for configurable input settings, However, those functions work with something called Variant. Docs does not say much about it. I'm not sure how it really work. Only thing I know is that Variants can be almost any build-in type but if I use file.get_var (which returns Variant) how I can get specific type from it ?

ps. I had never think I can have so many data-type related problems in dynamic-typed language... What an irony.

Garrom Out

I do not know how to convert types directly, but if you are using GDScript then you can check to see which type it is using code like this:

var expected_float = file.get_var() if expected_float is Float: expected_float *= 0.25

Then you can just use the variable like normal. Using code like this has worked nicely for me, back in Godot 2 at least.

Since you are using InputEvents, you should be able to do something like this:

var event = file.get_var() if event is InputEvent: "Do whatever you need to do here"

ps. I had never think I can have so many data-type related problems in dynamic-typed language... What an irony.

Dynamic languages try to make data types easier to manage, but sometimes they make it a lot more complicated :lol:

@TwistedTwigleg said: I do not know how to convert types directly, but if you are using GDScript then you can check to see which type it is using code like this:

var expected_float = file.get_var() if expected_float is Float: expected_float *= 0.25

Then you can just use the variable like normal. Using code like this has worked nicely for me, back in Godot 2 at least.

Since you are using InputEvents, you should be able to do something like this:

var event = file.get_var() if event is InputEvent: "Do whatever you need to do here"

ps. I had never think I can have so many data-type related problems in dynamic-typed language... What an irony.

Dynamic languages try to make data types easier to manage, but sometimes they make it a lot more complicated :lol:

So if I use file.get_var I don't have to convert it from Variant type ? It is already in original type ? If not, your code ... var expected_float = file.get_var() if expected_float is Float: expected_float *= 0.25 ...will never execute because expected_float is Variant (according to Docs, variant is return type of file.get_var()) and you expecting it to be float.

@TwistedTwigleg said: Dynamic languages try to make data types easier to manage, but sometimes they make it a lot more complicated :lol: Yeah, I kind of found out myself already... but thanks for approving it...

Garrom Out

file.get_var() will never return a Variant. If you read Variant in the documentation it just means that the variable can be of any type. As a consequence you may want to check what type the returned data is.

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:

@Unnamed and @TwistedTwigleg Thanks you a lot guys, now it makes sense, now I understand.

@GarromOrcShaman said: Hi all. I'm going to use file.get_var and store_var to store input events for configurable input settings

You should probably use ConfigFile to store configuration settings. It automatically serializes complex types such as input events and colors.

@Calinou said:

@GarromOrcShaman said: Hi all. I'm going to use file.get_var and store_var to store input events for configurable input settings

You should probably use ConfigFile to store configuration settings. It automatically serializes complex types such as input events and colors.

Thanks, I did not know about this, anyway I'm glad I finally got it working and I really don't want to rewrite it with use of ConfigFile.

4 years later