How to have only two bullets on the screen at a time?

:information_source: Attention Topic was automatically imported from the old Question2Answer platform.
:bust_in_silhouette: Asked By SpacePog

I’m making a shoot em’ up game and I wanted to be able to have only 2 bullets on screen at a time.
Could someone help me with it?
Info: The bullets are created by the player node as an instance of the bullet’s scene.

:bust_in_silhouette: Reply From: klaas

Hi,
there are many ways to archive this. One way is to use groups.

https://docs.godotengine.org/en/stable/getting_started/step_by_step/scripting_continued.html?highlight=group#groups

put your bullet in the bullet group, then do something like this

func shoot():
    if get_tree().get_nodes_in_group("bullets").size() < 2:
      #instaciate bullet here
      #dont forget to put them in the bullet group
      pass
   else:
      #there are allready 2 bullets on the way, no bullet gets spawned
      pass