How to move all instances each time an action is performed?

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

I want to move all instances (after being spawned) by some distance each time my player shoots a bullet.

extends KinematicBody2D

var motion = Vector2(10,0)
var current_pos = self.global_position # saved current position

func _ready():
	self.global_position = Vector2(85, 490)
    #starting position when spawned
func _process(delta):
	if Global.is_step == true:#true when player shoots a bullet
		current_pos = self.global_position 
        # save current position each time for each instance
		set_physics_process(true)
		Global.is_step = false
		
func _physics_process(delta):
	if self.global_position.x < current_pos.x + 100:
		self.global_position += motion

I wanted to spawn each instance one by one when the player shoots. But the above code works only for the first instance produced and the rest of them stay in the position where they were spawned. I tried using ‘self’ but it still doesn’t work. I am a beginner godot game developer so I’d like you to help me out with this.

:bust_in_silhouette: Reply From: njamster

But the above code works only for the first instance

Because it has the highest position in the tree, that instance calls -process() first. And sets Global.is_step back to false, before the other instances are processed.

Whatever part of your code sets Global.is_step to true should also be responsible for setting it back to false after X frames (10 in your example).

Thank you for helping me.

rAyMaX | 2020-04-17 16:52