Pointers in GDScript
GDScript doesn’t have explicit pointers like Go, but we can simulate similar behavior using references to objects. Here’s an example that demonstrates the concept:
To run this script, save it as pointers.gd
and attach it to a Node in your Godot scene. When you run the scene, you’ll see the output in the Godot output panel.
In this GDScript version:
We use a
Reference
object to simulate a pointer.Reference
is a built-in Godot class that’s passed by reference.The
zero_val
function works similarly to the Go version, not changing the original value.The
zero_ref
function changes the value in theReference
object, which is reflected in the calling function.Instead of printing memory addresses, we print the object’s instance ID, which is a unique identifier for each object in Godot.
We’ve added a custom
IntRef
class that extendsReference
to more closely mimic the behavior of the Go example. This class could be used instead of theReference
with metadata approach in the_ready
function.
While GDScript doesn’t have direct pointer manipulation like Go, this example demonstrates how you can achieve similar behavior using object references.