I know how it feels to be in your shoes as I was a few years ago in a similar situation (court). Actually three times for different reasons.
The first time I felt like I was going to throw up, shaking like a leaf, just a few feet away from my father, to see the person who should have never allowed for his daughter, to do what I had to so he wouldn’t further destroy my life with his gambling. Still he was trying to be nice, please what a con. Was pure evil and the most awful thing a parent can do, to see their child suffer.
Your ex is doing the same thing to you, he is playing the victim, the hurt one, now he says he has a gambling problem I feel more out of convenience , for the court’s sympathy; to save his behind. A role that many cg in denial play real well to get enablement. The minute you start feeling sorry for him, start feeling like you haven’t given your all, is the addiction getting through your wall and I would change that frame of thinking real fast or you’ll do something you’ll regret.
Continue on your recovery, don’t communicate with him, this includes writing a letter which he will misconstrued to think there is hope between you two. The best thing I feel you should do is let him live his life, let him gamble, let him take care of himself. He is not your husband anymore, it is time you let go.
Your unhappiness is not because of anything you didn’t do, it’s because you tried so many times and your ex just kept gambling. He is the one who made the choice to gamble, to live in his car and the only one who can get himself out of the mess he created, not you or anyone else.