Ben Affleck took his real-life rehab and sobriety seriously while playing an alcoholic for the upcoming movie “The Way Back.”

While filming the sports movie, Affleck was in rehab getting control of his alcoholism. The star has been candid about his sobriety struggles in the past and his co-star, Will Ropp, revealed that Affleck didn’t take any chances during filming.

“Everybody was very transparent from the beginning that he was in rehab at the beginning of the film,” Ropp explained to People at the Los Angeles premiere of the movie.

He added: “He had to have a sober liaison that would bring him to set and that would bring him back from [the] set.”

BEN AFFLECK SAYS EX JENNIFER GARNER AND HE ARE 'RESPECTFUL' TO EACH OTHER AND 'GET ALONG' FOR THEIR KIDS

In the drama, the 47-year-old former Batman actor plays Jack Cunningham. The character is a former basketball player who abruptly left the game. Years later, he’s forced to confront his own alcoholism when he’s tasked with coaching a basketball team at his old school.

Ben Affleck plays a struggling alcoholic in his new film 'The Way Back.'

Ben Affleck plays a struggling alcoholic in his new film 'The Way Back.' (Paras Griffin/Getty Images for Warner Bros.)

The parallels between the character and Affleck’s real-life situation were not lost on him.

“There are some things about this character I really could connect to — being a recovering alcoholic, going through family strife, a divorce,” he said, referencing his split with Jennifer Garner.

“You try to bring your own life experience to the parts that you can use your imagination on the other parts,” he continued to People at the premiere.

The actor went on to call the experience “cathartic” for him.

“The potential for a movie like this is to really inspire somebody, to move somebody,” he said. “Sometimes you do movies, you go, ‘Okay, it’s a thriller. They’re thrilled. And then they go home and they’ve forgotten about it.’ My goal with this was to make something that would feel enduring and lasting.

BEN AFFLECK'S INTERVIEWS ABOUT JENNIFER GARNER HAVE LEFT HER BOYFRIEND JOHN MILLER 'UNCOMFORTABLE': REPORT

“Not everybody is going to come in and think it’s great,” he added. “But for those who do, and for those who may be moved by this, the idea that you can face hard things and get better, I’m really proud of that.”

Ben Affleck had an emotional moment while acting out a scene in 'The Way Back' that mirrored his real life.

Ben Affleck had an emotional moment while acting out a scene in 'The Way Back' that mirrored his real life. (AP)

According to the film’s director, Gavin O'Connor, Affleck himself had a very emotional moment on set while playing a scene that reflected his real life a bit too much. The scene involves Cunningham making amends with his wife.

“It was probably the second take, Ben just had a breakdown. I’m getting chills thinking about it. It was like the dam broke and everything came out,” O’Connor told the Associated Press.

He added: “I just remember the crew, everyone was frozen, watching him bear his soul. It was obviously real. A lot of things that he probably had to say in his own life, or maybe he had said, I don’t know.”

CLICK HERE TO GET THE FOX NEWS APP

Affleck told the Associated Press: “Unfortunately, I had actually lived that life and done the research. I brought a certain perverse expertise because I knew what it was like to feel in thrall to a compulsion that wasn’t good for me.

"I knew how hopeless that can feel. And I knew how enormously frustrating it is. But I also knew something really important, which is: People get better. You can get better.”

Fox News' Melissa Roberto contributed to this report.