Barracks or a building that spawns troops

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

I’m working on a 2d rts type of game, as always it needs a barracks I’m new and after searching for a long time I figured I’d come and ask you guys. I have a troop model, a building/barracks model and a simple ui. I want to be able to queue troops and spawn troops.
I’m new to godot and game dev as a whole. I understand this is a big step but this is the way I learn.
your help will be appreciated :).

:bust_in_silhouette: Reply From: skysphr

Probably the easiest way to accomplish this is to:

  • Preload the troop scene(s) in the barracks script.
  • Add a variable for the number of queued troops. If they need to be of different kinds, you could use an array instead.
  • Create a timer and connect its timeout signal it to a function where you pop an item from the queue (or simply decrease the number of queued troops), then instance a troop, then reset the timer if anything is still left in the queue.
  • Connect your UI button to a function where you add an element to the queue and start the timer if there were previously no elements.