This is an idea common to science fiction, and perhaps made most famous by the Matrix series of films. However, the question goes back much further in history, and is one of the fundamental problems of philosophy. How can any of us know that our world or our experiences are really real? Rick’s question is similar to that of Zhuangzi in his famous passage about the butterfly dream:
Once Zhuangzi dreamt he was a butterfly, a butterfly flitting and fluttering around, happy with himself and doing as he pleased. He didn't know he was Zhuangzi. Suddenly he woke up and there he was, solid and unmistakable Zhuangzi. But he didn't know if he was Zhuangzi who had dreamt he was a butterfly, or a butterfly dreaming he was Zhuangzi. Between Zhuangzi and a butterfly there must be some distinction! (2, tr. Burton Watson 1968:49)
In Western philosophy, this question of what really exists led the philosopher Descartes to write the famous saying ‘I think, therefore I am.’ In other words, when a person can be sure of nothing else, the mere fact that they are thinking proves that their own mind, at least, does exist in some form.