Your code seems correct, I think the issue is arising with the fact you're preloading your Bullet scene in the same line that you're instancing it. Since it's being immediately instanced, the "preload" isn't able to load it before hand. Here's how I would do it:
const Bullet = preload("res//:Bullet.tscn")
func _process(delta):
if Input.is_action_just_pressed("fire"):
var muzzle = get_node("Muzzle")
var bullet = Bullet.instance()
muzzle.add_child(bullet)
bullet.global_position = muzzle.global_position
insert your other variables here
From a tutorial project however, I used this code:
const Bullet = preload("res//:Bullet.tscn")
func _process(delta):
if Input.is_action_just_pressed("fire_bullet"):
fire_bullet()
func fire_bullet():
var main = get_tree().current_scene
var instance = bullet.instance()
main.add_child(instance)
instance.global_position = muzzle.global_position
bullet.velocity = Vector2.RIGHT.rotated(gun.rotation) * BULLET_SPEED
bullet.velocity.x *= sprite.scale.x
bullet.rotation = bullet.velocity.angle()
bulletTimer.start()
the reason why the bullet is instanced as a child of the main scene as opposed to the gun is so the bullet moves independently of the gun. If you instance the bullet as a child of the gun, you would soon find the bullet would move when the gun moved due to the nature of children in Godot.
Hope this helps!