0 votes

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

Godot version 4.0 beta 7
in Engine by (18 points)

2 Answers

+1 vote
Best answer

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.

by (19,268 points)
selected by

That said, Gluon's answer is likely the simplest way to get your spawn points to "travel" with the player...

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 startspawnpositions = [], 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

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

0 votes

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.

by (3,321 points)
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.