+1 vote

I would like to loop though the values in constructed PoolVector2Array/PoolVector3Array's.
However in the docs, "Get" method is not available.
Is there anyway to do this currently?

extends Node 

var pos2d = PoolVector2Array()
var pos3d = PoolVector3Array()


func _process(delta):
    #pos2d = [(10,10),(10,40),(30,40)]  # <- Is  this possible?-- Expected ')' in expression
    #pos3d = ((10,10,10),(10,40,10),(30,40,40))   #<- Is  this possible?-- Expected ')' in expression

var p2_1 = Vector2(10,10)  
var p2_2 = Vector2(10,10)

var p3_1 = Vector3(10,10,10)
var p3_2 = Vector3(10,40,10)

pos2d.append(p2_1)
pos2d.append(p2_2)

pos3d.append(p3_1)
pos3d.append(p3_2)


# No "Get"method available

for p in pos2d:
    print("pos2d" + "-" + p + " = " + pos2d[p])

for p in pos3d:
    print("pos3d" + "-" + p + " = " + pos3d[p])
in Engine by (49 points)

1 Answer

+1 vote
Best answer

Here you go:

var pos2d = PoolVector2Array()

func _ready():
    pos2d = [Vector2(10, 10), Vector2(10, 40), Vector2(30, 40)]

    print("Array (initial):\n", pos2d, "\n")

    var p2 = Vector2(10,10)
    pos2d.append(p2)

    print("Array (after append):\n", pos2d, "\n")

    print("Array (1-by-1):")
    for entry in pos2d:
        print(entry)

The 3D-case is analogous but uses Vector3 instead.

by (10,327 points)
selected by
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to webmaster@godotengine.org with your username.