The Godot Q&A is currently undergoing maintenance!

Your ability to ask and answer questions is temporarily disabled. You can browse existing threads in read-only mode.

We are working on bringing this community platform back to its full functionality, stay tuned for updates. | Twitter

0 votes

Hi, if I have a PoolVector2Array and I only want a for loop using the first 2 vector setting. How could I code that. For example, I have many spawn positions in a PoolVector2Array, and I want different enemy be spawn at different position. However, the array[0,1] is not recognised by the editor, if some one could show what the right expression is, that would be helpful, thank you.

var enemyPosition =PoolVector2Array( [ Vector2(364,-496), Vector2(627, -496), Vector2(627,-353)])

func set_spawn_enemy (spawnPosition, spawnType):
    for i in (spawnPosition):
        var enemy
        enemy = (spawnType).instance()
        enemy.position = i

func _on_Hotspot_body_entered(body):
        if == "Player":
            set_spawn_enemy(enemyPosition[0,1],  enemyTypeA)
Godot version v3.2.3
in Engine by (266 points)

2 Answers

0 votes
Best answer

I have tried a few things and finally managed to make it work with

set_spawn_enemy([enemyPosition[0],enemyPosition[1]],  enemyTypeA)

that way, the enemy is spawned in the first 2 positions in an array.

by (266 points)
0 votes

This is because you're trying to use the [0, 1] array in order to access something from the array enemyPosition instead of passing the array by itself in as an argument. Try set_spawn_enemy([0,1], enemyTypeA) instead.

Edit: This also means that you must access enemyPosition inside the set_spawn_enemy function as well since you're not passing it as an argument. So either you change the assignment line to enemy.position = enemyPosition[i] or you change the function call to something like set_spawn_enemy([enemyPosition[0], enemyPosition[1]], enemyTypeA). It's not possible to slice PoolVector2Arrays.

by (8,550 points)
edited by

Thanks for answering my question, I have tried your suggestion but only [0,1] without an array name, the code is not working. I have tried a few approaches and finally made it work.

Sorry, I should have been more clear with my initial answer. There's no easy was to access a set of values from a PoolVector2Array, since you can't slice the array with another array.

Thanks for the updated answer.

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.