Many of you know what an ex machina is already (and why to avoid one) but for those of you who don’t, I’ll explain.
Ex machina is a shortened form of deus ex machina, which means “god out of the machine.” In ancient Greek plays, the major conflict of the play would be resolved with a god being lowered to the stage (via a machine), who would then fix everything.
Though popular in ancient Greece, this plot device is no longer encouraged. In fact, don’t do it. Ever.
Your readers spent all this time watching your protagonist fight the antagonist, and they want to see your protagonist triumph in those efforts. They don’t want your protagonist to win because of some coincidence or the intervention of a third party out of the blue.
It’s no fun. And it’s often a sign that the writer didn’t think through the plot enough to know how the protagonist was going to triumph. For example:
“Oh, my character is in a sword fight, but the villain is way better than my character. I know, the wall they’re fighting on crumbles at the right moment and the villain falls to his death.”
Fortunately, if you find you’ve got an ex machina, it’s not all that difficult to fix. Granted, you may need to do some serious rewriting, but you don’t have to come up with something totally new.
All you need to do is make sure your protagonist has some agency in the situation, and make sure that your reader already knows that an event like the one you have planned could happen. Agency + awareness = fixed ex machina.
For example, the latest Jurassic movie, Jurassic World contains what some have so wittily described as a dino ex machina. Just when the bad dinosaur seems like its going to kill the good dinosaurs, a mososaur jumps out of the water and eats the bad dinosaur.
They could have fixed this easily without losing too much shock value. If our friends Blue the velociraptor and her buddy T Rex had seen the mososaur cruising hungrily in its pen, and pushed the Indomitus Rex towards that pen…
Voila. No more dinosaur out of the machine.
It’s not even necessarily a bad thing for your conflict to be resolved this way. Some may have preferred to see Blue and T Rex kill Indomitus Rex themselves, but there are all sorts of reasons you might not want your protagonists to be the ones who kill the antagonist (assuming the antagonist has to die).
Your antagonist may be immortal for some reason, and therefore the only way to “kill” it/him/her is for demons or other things to drag the antagonist back where ever it came from. As long as it’s not a coincidence that these gatekeepers showed up just then, and your protagonist has some part in getting them to show up and do what they do, you’re good.
This happens a lot in Disney movies, where parents don’t necessarily want their children looking up to a character who has killed somebody, but the villains in these movies are often evil enough that they deserve to die. Anastasia and the new Frog Princess movie both end this way.
So if your antagonist must be defeated by a third party, (angels, a crumbling wall, a hungry dinosaur) just make sure your reader knew it could happen, and that your protagonist helped make it happen in some way.