+1 vote

Hello,

In a normal Array, there is a fuction called has(), that checks if there is a value in the given array.

However, in the PoolVector2Array, I can't find a function that does something like that.

Does anyone have an idea how to check for a value in a PoolVector2Array?

Kind Regards, sian2005

in Engine by (26 points)

2 Answers

+2 votes
Best answer

You can try with this helper function:

static func poolvector_has(poolvector, value):
    for v in poolvector:
        if v == value:
            return true
    return false

if poolvector_has(your_vector, value):
    ...
by (29,088 points)
selected by
+1 vote

Hello,

Given the official documentation, there is no such method on PoolVector2Array.

However, in GDScript, you can type cast values using the as keyword :

var vector2_pool = PoolVector2Array([Vector2(0, 0), Vector2(1, 0)])

if vector2_pool.has(Vector2(0, 0)): # will throw error
    print("true")

if (vector2_pool as Array).has(Vector2(0, 0)): # properly print true
    print("true")

Please read more about static typing here.

Best regards, Leirda

by (20 points)

Using as like this will duplicate the entire array just to call has.

Beware of the performance implications this could have. While this is a valid solution, this could easily become a bottleneck if your array has more than a few dozen elements.

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 Frequently asked questions and 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 [email protected] with your username.