With bullet reference:
var _bullet = null
func can_shoot():
return _bullet == null or not _bullet.is_visible()
func _process(delta):
if Input.is_action_pressed("shoot") and can_shoot():
[...] Get the position and direction where you want
# If the bullet doesn't exists yet, create it
if _bullet == null:
_bullet = preload("bullet.tscn").instance()
get_tree().get_root().add_child(_bullet)
# Fire bullet
_bullet.spawn(new_position, new_direction)
Then you can disable your projectile when it hits something or goes too far away.
Actually, with groups the code is simpler, but you have to put the bullet scene into a group,
and you also have to destroy it with queue_free()
when it hits something, and recreate it each time you need to shoot.
func can_shoot():
return get_tree().get_nodes_in_group("bullets").size() == 0
func _process(delta):
if Input.is_action_pressed("shoot") and can_shoot():
var bullet = preload("bullet.tscn").instance()
get_tree().get_root().add_child(bullet)
[...]
bullet.spawn(new_position, new_direction)
Actually with only one bullet, any of the options do the job quite well.
In both examples I assume there is a spawn
function in the bullet, it should set the position and direction of it (but I assume you have this already).