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

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.
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.