I'm currently working on a side project in Unity while in school majoring in Game Development. I'm running into an issue with the seed I use for map generation. I can choose to turn random seed generation on or off, and if off, I can input a seed for the map to be generated from. However, the problem I'm running into is that when I turn off random generation and input a seed, the user inputted seed, despite being identical to a randomly generated one, produces a different map than the randomly generated seed.
For example, say my random seed generator script spits out the string "hollow tasty boardwalk". I get the hashcode of that string and slap it into System.Random(). However, if I turn off random generation and type in "hollow tasty boardwalk" manually, the code still just takes the hashcode and puts it into System.Random(), but the map generated is completely different.
Here is the code used to seed the random generator:
public string seed;
public bool useRandomSeed;
public SeedGenerator gen;
if (useRandomSeed)
{
gen.GenerateSeed();
seed = gen.seed;
}
System.Random pseudoRandom = new System.Random(seed.GetHashCode());
If useRandomSeed is false, I just give it a value in the editor, since it's public. So like I mentioned earlier, let's just say for example my GenerateSeed() method sets gen.seed to "prickly swift hatbox". That in turn sets the seed in my MapGenerator script here to "prickly swift hatbox" and uses that to see the random number generator. However, if I manually set the seed to "prickly swift hatbox" from the Unity editor (or, as I plan to do in the future, a text window within the game), the map generated is completely different.
I suspect it has something to do with GetHashCode, as I'm probably misunderstanding how it works (I'm still pretty new to this). If that's the case, I'm not really sure of a simple workaround to turn my seed string into an int that the random generator can use. Can anyone help me confirm this issue and/or point me to some resources for a workaround? I'm pretty adamant about using a word string for the seed since it's a lot more user friendly that a lump of numbers. Thank you in advance!
Edit: The problem actually appears to be within my SeedGenerator script. So basically, it generates a list of words from a text file with \n separating entries. Either in the text file or in the code that creates the phrases, an invisible character exists that effectively makes the randomly generated seeds completely different than the user-inputted ones, since the user can't see/type that invisible character. Now I just have to find out where that character is and how to stop it!
Edit 2: The problem is in my adjective text file itself (the problem doesn't occur with nouns). Each word in the text file is separated by an "enter" key press, and when the file is read, it is split with "\n". For some reason, this leaves in an extra, invisible character at the end of each word. However, in my noun text file, everything appears to be separated by "shift+enter", and they have no invisible character afterwards.
Now, here's where things get interesting. Copy/pasting a list of "shift+enter" spaced words into a new text file changes the "shift+enter"s to plain "enter"s, causing the invisible character to appear. This is where I initially/unintentionally caused this error, as my adjectives are copy/pasted from various lists of common adjectives I found across Google. Using String.Trim in a foreach loop after reading from the file and slapping the words into an array of strings doesn't cut off the invisible character, so that's a no go. Copy/pasting the invisible character, interestingly, turns it into a normal "space". I have no clue why. So far, the only solution I can come up with is manually going into my adjective file and re-spacing all of the ~400 entries with "shift+enter".
Edit 3: My messy but functioning fix was to manually go through my adjectives list and replace every return with a space, and have my script split the file with ' ' instead of '\n'.