Invalid get index 'Marker2D' (on base: 'Array[Node]')

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By qwenough

Hi guys, i’m new, i want to make enemy spawn positions move with the player, but i can’t do it. I wrote this code in an attempt to achieve my goal, but I just can not understand what this error means. Сan someone explain to me why it could appear or say that I do everything wrong and suggest me how to do it?

var start_spawn_positions

func _ready():
	start_spawn_positions = spawnPoints.get_children()

func _physics_process(delta):
	for i in spawnPoints.get_children():
		i.global_position == start_spawn_positions[i].global_position
		i.global_position += player.global_position

screenshot:
http://ibb.co/BKJzDxh

:bust_in_silhouette: Reply From: Gluon

If you make the spawn positions part of the player scene they will move with the player. Alternatively you can attach them as child nodes of the player scene and again they will move with the player.

:bust_in_silhouette: Reply From: jgodfrey

Your error comes from the fact that spawnPoints.get_children() returns a collection of actual child nodes - not a set of array indices.

So, this code…

start_spawn_positions[i].global_position

… ends up resolving to something like:

start_spawn_positions[Marker2D].global_position, which doesn’t make any sense as an array index needs to be an integer, not a node.

That said, Gluon’s answer is likely the simplest way to get your spawn points to “travel” with the player…

jgodfrey | 2022-12-08 00:16

Thanks, i updated the code and added a counter for the array index, but now i have a different problem. The spawners fly far away instead of moving with the player like I can do on the scene editor. Any ideas how to fix this? Or maybe there is a better way to implement what I want.

P.s. I took the initial values of the position of the spawners in the start_spawn_positions = , so that I could reset them every cycle, and add the value of the player’s position.

var points
var start_spawn_positions = []

func _ready():
	points = spawnPoints.get_children()
	start_spawn_positions = points

func _process(delta):
	var i = 0
	for Marker in points:
		Marker.global_position = player.global_position + start_spawn_positions[i].global_position
		i += 1

qwenough | 2022-12-08 11:33

Remove that script entirely and see if you get the behavior your desire.

timothybrentwood | 2022-12-08 14:22