Set variables in an array dependent on which scene the player is in.
This may be kind of a confusing issue, but I'll do my best to explain my problem. I am making a game with many scenes (will probably end up at around 50 to 60). At the end of each stage I have a scoring system that awards 1, 2 or 3 gold stars depending on how well the player did. I then send this value to an int array to be saved. The number of the array that is saved needs to be dependent on which scene the player is currently on. I have a system that works, but will end up being very text heavy.
function SetGold(goldCount : int)
{
if(Application.loadedLevelName == "1-1" && goldCount > goldArray[1])
{
goldArray[1] = goldCount;
}
if(Application.loadedLevelName == "1-2" && goldCount > goldArray[2])
{
goldArray[2] = goldCount;
}
//etc...
//etc...
//etc...
Save();
}
This works fine, but I would need to copy, paste and change values many times. This is no big deal if I only have to do it once, but if I need to alter my code later on, I will need to copy, paste and change values all over again. So I'm wondering if there is a simple method that will allow the script to check the level and set the corresponding value to the array automatically.
Answer by jgodfrey · Jun 24, 2016 at 10:08 PM
There are lots of ways, but one might be to use a Dictionary for your storage, where the key is the "level name" and the value is your totaled INT for that level.
I'm not too familiar with dictionaries, but that does sound like what I might need. Something that pairs a specific string with an int. I'll look up more info on dictionaries, but wouldn't I still need to duplicate code over and over again because each level name is a different string that would need to be referred to?