+1 vote

Hey guys,

I would like to know what you would recommend as a spawning mechanism for units when they get "produced" in a building. Imagine a classic unit creation like in Command & Conquer or Age of Empires.

I see the problem that you need to calculate the position of the created unit. However, sometimes it can collide with other bodies. And sometimes, if the building is blocked from all sides, there is no chance to place the unit. The problem is actually bigger than it seems.

So I came up with this solution. I calculate the tiles around the building which represent the possible spawning areas. My idea is to create these tiles (12 in the screenshot) as Area2D nodes. Then, every time a unit is to be produced, I calculate a free tile using getoverlappingbodies() from the Area2D class. This way I know if other buildings (StaticBody2D) or units (KinematicBody2D) are blocking the exit. And if all exits are blocked, I can tell the player to free the exits before he can produce more units
enter image description here

What do you think about that? Is this a good solution or do you know something simpler?

Godot version 3.2.2
in Engine by (13 points)

1 Answer

0 votes

This should work.
You cloud make it simpler if the spawned nodes cannot move directly (if some function gets called on the spawner for that to happen). In that case, the spawner could just remember what lies around it.

by (2,568 points)
Welcome to Godot Engine Q&A, where you can ask questions and receive answers from other members of the community.

Please make sure to read Frequently asked questions and How to use this Q&A? before posting your first questions.
Social login is currently unavailable. If you've previously logged in with a Facebook or GitHub account, use the I forgot my password link in the login box to set a password for your account. If you still can't access your account, send an email to [email protected] with your username.