When it comes to the reason why we have nightmares, we're still debating that. It's a new area of research, nightmares. And the way I like to think about it is, our brain - we have stress during the day and our brain needs to learn to process this stress.
In learning to know other things, and other minds, we become more intimately acquainted with ourselves, and are to ourselves better worth knowing.