So, here’s the scenario. When my game boots up, I set a specific seed. In this case, the seed I have chosen is the number “5147”. I set it using Random.InitState(seed) in my GameManager’s Start function. I then play the game as normal. I go through the menus, load into my first level, and go up to a chest that should be random. My first run-through, the chest had (for the sake of discussion) Object A. I restart the game, go through the motions again, but this time the chest had Object B.
This is obviously an issue, since the whole reason I want to have seeds in the first place is so that if players really like a run of a game, they can jot down the seed and play it again whenever they want to - exactly as it was the first time they played it.
I assume that the reason that the object was different each time was because I reached it at different times? Like it wasn’t frame perfect timing, obviously. But, if the Random function is still frame-dependent, then even when the seed is set, that’s still effectively random, isn’t it? There’s no way that Unity expects us to be perfectly-on-frame just to get a seed functioning as intended?
What is the fix for this? How can I make sure that my game generates the same each time using a seed? Am I misunderstanding how Random.InitState functions? I’d appreciate the help!
Assuming you re-seeded it to 5147 at the beginning of the second run as well, it should be giving you an identical set of results back. This assumes you had an identical amount of random events happen though. If you say, killed a skeleton that dropped some random loot on one run but not the other, then the chest’s random roll would be a different value if you’re generating the loot on use rather than on creation during your level generation.
Yep, that seems to be my problem here. I have a LOT of random events in my game. It’s a rogue like. There could be any number of random events in between the applied seed and the loading of that chest.
What I think I’m going to do now instead is to use System.Random.Next to load objects. I’ll have to be careful, but I think that should work.
Generate all of the important “seeded” random things, like what’s in the chest or what monsters drop what loot, during level generation instead of as they occur.