Developers would often publish them in handbooks dedicated to cheat codes, or have them hidden somewhere in-game.
Other times they’d have someone take the role of a regular user in a game forum and just randomly leak the code, so it seemed like someone just eventually figured it out and it would gradually perpetuate to other forums / word of mouth etc.
Rarely did players stumble upon cheat codes organically.
Latest Answers