New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Allow using generics with Variant.CreateFrom() and Variant.ConvertTo() #5762
Comments
The public static T GetProjectSetting<T>(string key, T defaultValue) {
if (!ProjectSettings.HasSetting(key)) return defaultValue;
var variant = ProjectSettings.GetSetting(key);
return VariantHelper.ConvertTo<T>(variant);
}
public static void SetProjectSetting<T>(string key, T value) {
var variant = VariantHelper.CreateFrom(value);
ProjectSettings.SetSetting(key, variant);
}
public static void SetTypedValue<T>(this ConfigFile configFile, string section, string key, T value) {
var variantValue = VariantHelper.CreateFrom(value);
configFile.SetValue(section, key, variantValue);
}
public static T GetValue<T>(this ConfigFile configFile, string section, string key, T @default = default) {
var variantDefault = VariantHelper.CreateFrom(@default);
var variantValue = configFile.GetValue(section, key, variantDefault);
return VariantHelper.ConvertTo<T>(variantValue);
} |
Variant only allows a few types and they can already be implicitly converted so I don't see the benefit of allowing Currently, because all Variant compatible types are implicitly converted, you can already set a project setting to an One of the inconveniences of using generic methods in here, which is the main reason that Variant was created to begin with, is that it would allow the user to pass any type (including types not compatible with Variant) which means if the type is not compatible it will fail when passing it to Godot, this means that while the code will compile it will fail on runtime and we want to move as many errors to compile time as possible to ensure safety. If we were to add those generic methods, we would have to use the I can see value on having a generic TL;DR: I don't think we want to go back to using generic types without constraints as Variant, since that's the reason we moved away from |
Thanks you for your comment! I guess having a good way to convert |
Implemented by godotengine/godot#68310 which adds this API: partial struct Variant
{
public static Variant From<[MustBeVariant] T>(in T from);
public T As<[MustBeVariant] T>();
} |
Describe the project you are working on
A C# library to create animations and effects with more complex tweens.
Describe the problem or limitation you are having in your project
I have some classes with generic to interpolate node properties, so user can create something like
So, I have a kind of class
class Property<T>
where T is float (or any other type compatible with Variant). Then I usenode.GetIndexed
andnode.SetIndexed
to update the node property, but I have first to convert a T value to Variant, in order to use these methods.Describe the feature / enhancement and how it helps to overcome the problem or limitation
If I had a
T Variant.ConvertTo<T>(Variant v)
andVariant CreateFrom<T>(T v)
methods, I could use in my library. And any other user in the same situation (convert variant to a generic type and the other way around) could use it too.Describe how your proposal will work, with code, pseudo-code, mock-ups, and/or diagrams
I have solved this with these two methods, that can be used (or improved, or ignored) to achieve it:
If this enhancement will not be used often, can it be worked around with a few lines of script?
I already have the methods working, but I think this can be useful for all the users.
Is there a reason why this should be core and not an add-on in the asset library?
It looks to my a lack in the Variant class than a extension
The text was updated successfully, but these errors were encountered: