Hello community, newbie here, asking a probably simple question. But then again I am learning.

Here is my case: I am trying to instance one scene (Kinematic2D) in a main scene(Node2D),

The object in the kinematic scene further referred as "enemy" is an object that moves on X axis based on a Vector2 + move_and_slide method., involving 3 ( speed, velocity and direction) variables. By default the object goes from left to right, and if i replace the direction variable from 1 to -1 it goes to the opposite direction

The main map, further referred as "world" is a node2d scene .

The plan is to instance the enemy multiple times in the world randomly at 4 Position2D spawning nodes.

For this i have three choices: 1. manually instance the enemy scene multiple times in the world scene, and change manually the exported direction variable for the right to left enemies. It works but it's not an option given the volume of instances i want done, Much like in the image bellow

  1. Code based instance. Create a second enemy scene with the direction parameter changed (right to left) and instance it when desired. It works but i'd rather avoid (if possible) creating duplicate scenes just for a changed parameter.

  2. Code base instance. Instance the same enemy in the world with the direction variable changed when necessary.

The problem here is that i can't ensure the direction parameter is changed just for the a certain instanced object, instead it changes the direction on the fly for all existing instances. Much like in the bellow screenshot.

What i would need, if possible is to spawn enemies with the necessary parameters that are not affecting the behaviour of the already existing instances.

Currently the code is looking like bellow:

enemy.gd

extends KinematicBody2D

const speed = 150
var velocity = Vector2()


func _physics_process(_delta):
	velocity.x = speed * globals.direction
	velocity = move_and_slide(velocity)

func _on_VisibilityNotifier2D_screen_exited():
	queue_free()

globals.gd --> this is a script i am autoloading along the app

extends Node
var direction = null

world.gd - this is the world, a Node2D scene with 4 Position2D nodes used to mark the places used for randomly spawning the enemies when a button is pressed (in this case)

extends Node2D

var myenemy = preload("res://enemy.tscn")
var rndvalue = null


func _on_LinkButton_pressed():
	randomize()

	rndvalue = randi() % 4	

	var e = myenemy.instance()
	get_parent().add_child(e)

	if rndvalue == 0:		
		e.position = $sl1.position
		globals.direction = 1	
	if rndvalue == 1:		
		e.position = $sr1.position		
		globals.direction = -1
	if rndvalue == 2:		
		e.position = $sl2.position		
		globals.direction = 1
	if rndvalue == 3:		
		e.position = $sr2.position
		globals.direction = -1

Can someone figure if it's possible to avoid changing the direction for all existing enemies when a new one is instanced? Or what i am doing wrong in my attempt (case 3, above)

Thank you in advance for your advices.

Best regards n.

enemy.gd

extends KinematicBody2D

const speed = 150
var velocity = Vector2()

var direction = 1

func _physics_process(_delta):
	velocity.x = speed * self.direction
	velocity = move_and_slide(velocity)

func _on_VisibilityNotifier2D_screen_exited():
	queue_free()

func set_direction(dir):
	self.direction = dir

world.gd

extends Node2D

var myenemy = preload("res://enemy.tscn")
var rndvalue = null

func _on_LinkButton_pressed():
	randomize()
	rndvalue = randi() % 4  
	
	var e = myenemy.instance()
 	
 	var dir = 1
	if rndvalue == 0:       
		e.position = $sl1.position
		dir = 1   
 	elif rndvalue == 1:       
		e.position = $sr1.position      
		dir = -1
	elif rndvalue == 2:       
		e.position = $sl2.position      
		dir = 1
	elif rndvalue == 3:       
		e.position = $sr2.position
		dir = -1

	e.set_direction(dir)
	get_parent().add_child(e)

I think it's using the 'global.direction' that's the source of your woes, in this case. I haven't tested the solution I've proposed, but hopefully you get the gist of my intent, here.

Hmm... this would have them all moving in random directions... this might not be what you want...

Hmz...right now, the game spawns only from right to left and also apparently only from a certain spawning point.

But the idea of connecting signals may actually prove useful for solving the dilema

Many thanks @GreyWolf117

Later edit...your code actually solved this matter. I had to reedit the world.gd script as following

extends Node2D

var myenemy = preload("res://enemy.tscn")
var rndvalue = null


func _on_LinkButton_pressed():
	randomize()
	rndvalue = randi() % 4	
	var e = myenemy.instance()
	var dir = 1		
	if rndvalue == 0:       
		e.position = $sl1.position
		dir = 1
		e.set_direction(dir)
		get_parent().add_child(e)
	elif rndvalue == 1:       
		e.position = $sr1.position      
		dir = -1
		e.set_direction(dir)
		get_parent().add_child(e)
	elif rndvalue == 2:       
		e.position = $sl2.position      
		dir = 1
		e.set_direction(dir)
		get_parent().add_child(e)
	elif rndvalue == 3:       
		e.position = $sr2.position
		dir = -1
		e.set_direction(dir)
		get_parent().add_child(e)

And now , lo and behold..the enemy spawning is actually working . Many thanks @GreyWolf117

2 years later