How does a composer get paid?

Hello, I would like to ask any of you how would a composer and any studio/dev find an agreement about getting paid on a game for making music?

Let me explain: Imagine a composer comes across a game that needs music. The game is still in development and there might not be money for music, so what are possible agreements and scenarios?

Would a composer and a dev agree somehow that if/when the game makes money, the composer gets his share?

I'm trying to understand how composers get rewarded when composing for an indie game because I myself am a composer and don't understand how the relationship works with any studios. I don't mind making music for free right now if there's some contract that states I get paid if the game works and sells, and if not, it would just be a nice addition to my portfolio.

Please let me know how it works or how you think it works. I have absolutely no knowledge about the subject and I really want to get ready to be involved in indie projects. Until now I have only made music for open-source/personal projects.