How can I act on an instance reference ? #27
-
Is it possible to directly call native methods on instances of Objects I created via gd ? Sample: func (m *Main) createBoid(id int) {
mob, ok := classdb.As[gd.RigidBody2D](m.MobScene.Instantiate())
if !ok {
fmt.Println("failed to cast!")
return
} I would Ideally want to have a reference to the mob, that I could call .setVelocity / .setPosition on. For context when I later use: b.gdObj.AsNode2D().SetPosition(position)
b.gdObj.SetLinearVelocity(velocity) it ends up exploding with: I may need to provide a reproducible example, I am using rwMutex on the boids, likely I screw up access in there, and it's not about the method linking at all. Will report back :) |
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 11 replies
-
I imagine you are storing the mob somewhere, in order to do this with a Godot reference, you will need to pin the lifetime of the mob to the class it is contained in. I am exploring a design on how to make this more intuiative. Note that since the instance inherits A good heuristic to keep things simpler when working with Godot references in Go, always try to use references temporarily (within your method call) before you return back to Godot, if you need to hold onto the reference for a longer time, store the reference somewhere inside a Godot managed class (or use a Godot reference like |
Beta Was this translation helpful? Give feedback.
Thanks, looks like you have found a bit of an issue here, as you need the node to be alive in Go in order to get the nodepath, I've added a function to
gd
(gd.AddChild
) that will workaround this, it will add the child and return the resulting NodePath (I've tested this and it looks like the rigid bodies are moving now).You can replace
with
Big thanks for reporting your experience here, it really helps us to identify the pain points and areas to improve our Go integration with Godot!