Variant utility functions
Ullanar opened this issue · 1 comments
For now there are hard to work with varians due to lack of documentation and utility methods
For example now trying to make simple 3d character controller. And ofc we need gravity setting which is Variant
.
gravity := gd.ProjectSettings(godot).
GetSetting(godot, godot.String("physics/3d/default_gravity"), godot.Variant(9.8))
And then we need, for example, handle falling state. Which means what if we are not on floor we need to work with Velocity.Y
and common case is
y := GetVelocity().Y() - gravity * delta
So delta is gd.Float
and gravity is gd.Variant
.
I spend several hours trying to understand how to cast them, searching through source code and still cant understand how to correctly work with math here
Variant.Interface()
returns any
but then how we can cast any and Float
So the issue is:
- How we can actually work with type casts?
- Maybe it is possible to make more such utility functions?
Even so, thank you so much for your great work. It was more than year since we are doing smth with godot and using Go goroutines in Godot it is the best birhday gift which we can even have
Thanks for sharing your experience here @Ullanar, you can convert the variant into a gd.Float
with a type assertion, ie.
var f = godot.Variant(gd.Float(3.14))
fmt.Println(f.Interface(godot).(gd.Float))
I've also just added a commit introducing methods that will do the same thing for value types (similar to reflect.Value
, they will panic if the type doesn't match).
var f = godot.Variant(gd.Float(3.14))
fmt.Println(f.Float())