This is kind of a generalized question and there are a number of ways to approach something like this.
There are two main things you need to accomplish:
- Keep track of how many coins exist / when the count changes
- Keep track of the node you wish to destroy / destroy when required
I am generally a fan of keeping counters. When a coin is created you increment the counter, when it is destroyed you decrement the counter. Once the counter reaches 0 you destroy the object.
In each coin you can increment the counter in the ready() function, as that will be called when the coin spawns. For decrementing you can use the exit_tree() function as that will be called when the coin is destroyed.
To free a node you must call free() somewhere on the node and it will be destroyed. These should be all the tools you need to get this to work.
A couple methods:
Simply have a variable in some controller node that your coins modify as required. This controller can check the number of coins every step with the _process() function and destroy your static body when it is zero. You may need to keep track as to whether or not you have already destroyed your node so it doesn't attempt to do it repeatedly.
You can attach signals between your coins and your controller and between your controller and your physics node. When coins get removed they signal the controller so it decrements the count. When the count reaches 0 in this signal the controller signals to destroy your desired node just the one time.
I find option 1 is simpler to understand but it is more prone to errors. Option 2 is the cleaner and more robust option but, at least for me, signals made my brain hurt just a little when starting out.
Give one of these a go. If you have further questions or come across a problem then follow up in this topic and we can help you out.