How to Avoid Using Deus Ex Machina
Avoiding deus ex machina is all about world-building. In any fictional tale, the author has to lay out a world – either a small stage within the real world (standard fiction), a made-up world (science fiction/fantasy), or a world from the past (historical fiction). Regardless of the genre, the author needs to explain implicitly, but clearly, how the world works and where its limits are. The resolution to the story has to emerge from within those limits.
In effect, the way to avoid deus ex machina is to begin with the end in mind. If you’ve written an ending where the hero needs a gun to escape, make sure that there’s a scene earlier on where the hero gets that gun. Or, if there’s an external character who shows up to save the protagonists from certain doom, make sure that this external character is well established and that when he or she arrives it doesn’t feel “out of nowhere.” Naturally, this may mean that you have to go back and revise the beginning of the story once you’ve figured out its ending!