Sir Ian McKellen spoke to schoolchildren about his life as a gay man and suffering homophobia.
The actor was working with a group of students at Severn Vale School, in Quedgeley, near Gloucester, on a play about homophobic bullying, and talked about coming out aged 29.
After an hour in a drama workshop, the 70-year-old said he was impressed with the play - which was performed in front of around 200 pupils from other schools - and was proud of the students for tackling issues around homophobic bullying in schools.
"Being gay was a topic that was never mentioned when I was your age. We had not really invented the word gay - at school I used to be called Oscar, after Oscar Wilde," he told the audience.
"If you were gay there was nowhere to go and no one to talk to, there was no other gay person as far as I knew.
"So to come back to school for the first time in 50 years and see this is heartening, to see that as a nation we have so rapidly grown up.
"When I was 29 it was illegal for me to make love, I had a boyfriend and we slept together but the law said that we should be in prison. It was very hard to walk out in the street and say to him don't touch me or brush your hand against mine, there may be a police man around the corner."
He said the greatest regret was that his parents never knew that he was gay.
Sir Ian also used the opportunity to speak about gay rights. And he slammed Radio 1 DJ Chris Moyles for using the word gay in a derogatory way "because he is a careless lout".