I'm messing about with another person's work (RegularPolygon2D: here) and I've found myself confused by some behavior related to export and PoolVector2Array.
I'm looking for a PoolVector2Array filled with the local coordinates for each of the RegularPolygon2D's vertices. Whenever I explicitly call points_get
from outside the node (in a C# script attached to a parent Control node), I get an empty list as a result. When points_get
is called by the implementation of RegularPolygon2D, however (in debug Print()
calls) the result is a correctly populated PoolVector2Array.
I've attempted to highlight the relevant code below, but you are welcome to look at a recent snapshot of the entire project here. The files worth looking at are
1) /addons/regularpolygon2d_node/RegularPolygon2D.gd
2) /MyHexGrid/Control.cs
3) The scene is /MyHexGrid/VisualHexGrid.tscn
Here's the C# code that gets an empty list (but I wouldn't expect it to, especially since the num_sides_get
call resolves correctly, printing 6):
public override void _Process(float delta) {
if (Input.IsKeyPressed((int) KeyList.K)) {
GD.Print("Printing from C#: "
, this.GetChild(0).Call("num_sides_get") // Prints 6 correctly
);
GD.Print("Printing from C#: "
, this.GetChild(0).Call("points_get") // Prints an empty list
);
}
}
Here are the additions I made to RegularPolygon2D.gd to achieve this:
export(PoolVector2Array) var pts setget points_set, points_get
...
[the following at the end of poly_pts(), used to generate the point locations based on the given polygon's size, where ppts
is the local name for pts
:]
...
points_set(ppts)
return ppts
[the following immediately after poly_pts()
is called in draw_poly()
:]
pts = poly_pts(p_size)
points_set(pts)
...
Setget:
func points_set(new_pts: PoolVector2Array):
print("points_set was called with points ", new_pts)
pts = new_pts
func points_get():
return pts
EDIT: Just for clarification, the exported pts
variable and associated setget block are mine, not the original author's.