Hey, im quite new to Dictionaries and Lists so i dont know if im missing something completely obvious but what is the difference between them and just having Int’s.
I’m making a game and in it you have a run down house that you can upgrade. So you can upgrade the things like the Desk and fix up the walls along with things on the outside of it and other rooms etc. Anyway the point is what is the difference between having a public int called Desk and having each int correspond to a version of the desk and a Dictionary that has the string as “Desk” and then the Int again correspond to the Desk version
to get a country code by name you just access the dictionary using the country name.
the key is what you use to access the value stored, in this case a prefix number.
A list is a series of things you can add more things to or remove easily and it’ll shrink and grow to fit.
A dictionary is a way to remap one thing to another, like a look up. So maybe you use GameObject as the lookup and it’ll give you back a script reference or some other bit of data you choose. Or maybe you want to make a translation of some kind.
Hey, thanks for all the responses an example code would be something like this
public static int Desk1;
public static int Desk2;
public static int Desk3;
public static int Desk4;
public static int Desk5;
public static int DeskSelected;
public void CheckDesk()
{
if(DeskSelected == 1)
{
DeskObj1.SetActive(true);
//etc etc
}
public void DeskLevelCheck()
{
if(Desk1 == 1)
{
Desk1Ver1.SetActive(true)
//etc etc
}
sorry that code is very basically done i just wanted to try and show my main point ahaha i promise i can actully code properly.
Anyway what would be the difference between having something like that and having them stored in dictionaries
Functionality? No difference.
Developer workflow? Huge difference.
A dictionary/list/etc. by itself won’t do much here - this is an XY problem.
The issue is less that you aren’t using a dictionary/list/etc. (though you probably still should) and more that you have different pieces of data associated with a “desk” that are split apart all over the script:
int Desk1;
GameObject DeskObj1;
GameObject Desk1Ver1;
For every “desk” you may want to add/change/remove, you have to update your code in at least 3 different places. Possibility more, depending on if you later need to associate other kinds of data with “desks”. This isn’t a scalable approach.
This is an ideal candidate for creating a model “desk” object that encapsulates all this data:
[Serializable]
public class Desk
{
public int ID;
public GameObject deskObj;
public GameObject verObj;
}
The [Serializable] attribute is optional, but adding that allows Unity to display this Desk object in the inspector, where you can set its properties there.
You can then create whatever collection you need out of these Desk objects - doesn’t specifically need to be a dictionary or a list. I’ll use a simple array in this example:
public class DeskManager : MonoBehaviour
{
public Desk deskSelected;
public Desk[] desks;
public void SetSelectedDesk(int deskID)
{
foreach(Desk desk in desks)
{
if(desk.ID == deskId)
{
deskSelected?.deskObj.SetActive(false);
desk.deskObj.SetActive(true);
deskSelected = desk;
break;
}
}
}
//These methods aren't needed anymore, since all they did was search for the correct "desk"-related GameObjects to toggle based on which "desk" int matched the "deskSelected" int.
//You already know what these GameObjects are ahead of time, as they're inside of the new "deskSelected" Desk object.
public void CheckDesk(){}
public void DeskLevelCheck(){}
}
They solve problems that you haven’t had yet. Like with a screwdriver, it’s easier to wait until someone has tried to drive in a screw with a hammer before you show them.
A C# List is just an improved array. Arrays/Lists are great when you have many similar variables, for example desk1, desk2, deck3 … up to desk10. Using those often involves 10 IF statements, which is a big pain and easy to mess up. Putting the variables into a single size-10 array makes things easier.
Dictionaries aren’t actually needed, but they’re fun. They let you use strings as array lookups like AnimalSounds["cow"]=""moo"; and print(AnimalSounds["horse"]);.