Assuming you're only concerned with horizontal movement, the simplest solution would be to have the player script launch a function in the bullet's script when the player instances it, and have the player's code plug in a direction variable as an argument in that function in the bullet's code:
So the bullet's script can have a variable called direction
var dir = 1
in the bullet's physics process, just set it to automatically travel upon being created:
func physicsprocess(delta: float) -> void:
motion.x = Speed * delta * dir
translate(motion)
translate( ) or moveandslide or moveandcollide, depending on what kind of node your bullet is. Mine is an Area2D, so I used translate(motion)
With that direction variable, you'll be able to use it to tell the bullet whether to flip_h or not:
if dir == -1:
$Sprite.flip_h = true
and if direction is a -1 it will make it travel left, but we'll need the player who creates the bullet to tell our bullet's script which value direction will be. So let's create a function here in the bullet script that the player will access when he creates a bullet.
Maybe something like:
setbulletdirection(d):
dir = d
that little "d" inside the parentheses is like a little teleporter that can receive variables from other scripts that call this function. I called it "d" but you can call it anything you like. Just know that it is a container that will receive a piece of info from the player.
Now in the player's script, figure out a way to tell your player's direction. Could be something like this:
var direction = 1
if Input.isactionjustpressed("uileft"):
direction = -1
elif Input.isactionjustpressed("uiright"):
direction = 1
Or you could do something similar with motion.x being greater than or less than 0 to set the direction variable. However you want to set direction to either 1 or -1, based on your player's movement
Now we create a variable to represent the bullet that we instance. I called mine bullet whereas the preloaded bullet was a constant, so I called that BULLET
const BULLET = preload("res://Objects/Bullet.tscn")
var bullet = BULLET.instance( )
then, before we add the child to the scene, we'll use our bullet variable to access the function in the bullet's script and plug in our own direction variable:
bullet.setbulletdirection(direction)
notice how I put the player's variable "direction" inside the parentheses where in the player script we have a "d"? Basically that makes the bullet's script plug in "direction" in place of that "d" and sets that value of either 1 or -1!
And then, finally we spawn the bullet:
getparent().addchild(Bullet)
After that you can set the bullet's position, but I'll not get into that now, assuming you already know how to do that. Hope this was helpful.