grow-graphics / gd

Go + Godot 4.3
https://learn.grow.graphics/
MIT License
244 stars 11 forks source link

Variant utility functions #34

Closed Ullanar closed 4 months ago

Ullanar commented 4 months ago

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:

  1. How we can actually work with type casts?
  2. 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

Splizard commented 4 months ago

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())