For those looking for the next cool thing to pick up with Dictionaries. Look at using a Dictionary with a Command pattern. and you can pass in an Action by type or Type and it will return a method you can then fire. This is how you can make quickly variable Command patterns that are manageable versus monster smelley Switch trees.
Arrays, lists, hashsets, dictionaries, stacks, queues... Nice video. There's so many ways to deal with data it's good to have the correct tool for the job. I wouldn't want to use a sledge hammer on a finishing nail.
@@DemaGraphexTube Hi I'm a noob myself but I believe PlayerPrefs only accepts floats, ints, and bools by default. I created a class that converts a list into serialized playerpref entries and another class that converts the entries back into a list, i'm sure you can do something similar for arrays
Queue was used in Unity Beginners Code Kit to limit audioSources to play as I understand. They have a singleton that instantiates limit number of prefab audioSources for particular group of sounds for example player. And use queue to remove first audioSource in queue and add it to end of the same queue when new sound is going to be to played.
This topic is too complicated for me, for now. But i watched it with greate interest, and save for later - i will defenetly watch it at least one more time, when I'll became more familiar with collections in C#. Thank You for this video, and all videos, You have done, they are greate! Very interesting, informative, useful and well done.
Nice explanation. So a HashSet is basically a List with unique items only. [Add, Remove]. Dictionary, self-explanatory. [Add, Remove]. Stack is LIFO (Last in first out) - [Push Pop]. Queue is FIFO. [Enqueue, Dequeue].
Relative performance matters as well. Searching a large list for an element is slower than a hash set or a dictionary, but accessing an element in an array is crazy fast by comparison. If you can use an array or list where the key is just the array index, performance will be much, much faster than a dictionary. In my experience with Unity and C# in general, the list has to get pretty big before a Dictionary starts to outperform it. Like a couple dozen items or so.
Thank you! This is a very useful video. Coming from a PHP background, the different types of collections in C# had me a bit puzzled, and this cleared everything up.
Initially the idea of TH-cam premiers seemed silly and pointless to me, but I stumbled into yours (for this video) and I must admit it was awesome seeing you there in the chatroom while you also talked us through in your video... it's like you finally hired an assistant. = )
@@teemuleppa3347 There's notifications that a that a premier video is scheduled in advance if you want to watch it live. For not-live ones you just gotta noodle around the video's page until you find the Replay Chat button. It even scrolls the chat history while you're playing the video.
Audio sounds much better than before but you still reduce the harshness by making a dip in the frequencies around 1000 - 2000 Hz. WIll make your voice a lot smoother.
Ive been a self though developer for more than 5 years... until this this day I had no idea what a hash set was... allways checking list to see if an item with a custom id was already there... oh silly me.... so yeah, thank you a lot.
The main reason/situation when I use HasSet instead of List is when i need to frequently check what's inside it. List.Contains() is an O(n) operation (that is, it takes longer the more stuff there is in the list), while HashSet.Contains is O(1), so it's constant speed no matter what. Dictionary.ContainsKey() is O(1) as well, but if you don't need to pair keys and values it's needless hassle.
Thanks for sharing. But I didn't quite get the use case for the temporary stack of items. Usually you just won't allow addition of new items to a full inventory instead of keeping them in a temporary place that you have to make a UI for.
It is useful if inventory is meant to hold stackable items. Inscope studios on youtube made set of tutorials on system like that with good explanation why.
@@dfhdgsdcrthfjghktygerte I'm not sure that I was clear enough. I'm talking about the "_overflowItems" collection. If this is a collection of items that didn't get into the actual inventory, where would they go in real scenario?
@@MTandi I don't want to bring extra confusion, i just briefly looked into his code but to me it seems the purpose of _overflowItems is not to keep extra items, but add the ability to pop 1 item and push another one in it's place in case collection is full. And it looks like the popped item is removed in the process but it can be put into different slot, bag, inventory page for example.
It's a system we have in pantheonmmo.com where extra items you get from quests/etc that are force added to your inventory go into an overflow, then when you free a slot you can put it in your actual inventory. The goal is to allow people to finish quests/etc and still get the items without having to clear a spot in the inventory or stop the progression until they do :)
Thanks for awesome practical video tutorial. I noticed that in editor you have hints like "2 usages", "frequently called" etc What's that and how do you get them displayed? Thanks and like p.s. I also like how you hide setter of instance property, nice touch! p.p.s. Whenever you use fade out effect, I am thinking that my iPad went to sleep due to long inactivity :D
A good beginner tutorial. But there are some missing elements from the info you provided and that keeps it at a beginner level. (Just trying to provide constructive criticism) What you may or may have not known: Lists are just fancy arrays. And for your inventory system it would have been better because dictionaries have a cost to calculate the hashcode of an object. Which isn't even relyable unless you implement it yourself or don't create duplicates. For inventory the tldr is keep an array. HashSets & Dictionaries (or hashmaps) use hashcodes to define the uniqueness with a checkup for doublechecking. Also when iterated over it the output is semi random (foreach) because its based on how the hashcodes got sorted. Also in you lose performance because you iterate over every entry even if that is not present. There are faster options for hashsets/maps that are linked that give greater iteration performance but they are not by default in c#. Also there are two types of queue. A stack that is first insert & last extract. Or a linked list which is first insert & first extract. Also sorted queues like treesets or priority queues that can sort the values based on a comparator. Also what you may should suggest later too is to mix them up because loads of developers in unity I have seen decide to use only 1. You kinda did that with your dictionary stack but still wanted to tell how important that is. Thanks for providing a good beginner level tutorial because they are not as present. ^^ I hope this does not sound as yelling because I just wanted to provide Info's that back then I wished I had known.
People just got used to call any static field/property that returns an instance of that object a singleton, but if you don't check/destroy duplicates it really isn't a true singleton. But still, most people don't like the if (instance != this) { Destroy(this) } pattern because you lose control of what's being destroyed unless you are careful. But of course, if you don't trust yourself or your team to be careful with it you just shouldn't mess with the destroy singleton pattern at all
I’m the guy that uses arraylists far too often. I like dictionaries and queue though too. I think after watching this most of the places I use arraylists I might use hashmaps just because o check for redundancies far too often.
When you used the dictionary as an example, in the method LoadItems(), in the head of ToDictionary(key and value) the variables before the arrow, =>, are defined nowhere locally nor as class variables, does that mean key and value can be used after the method call of ToDictionary, or does it have something to do with ToDictionary?
So, just a question (it's not related to this video tho), the underscore for private parameters is recommended? Because I saw in a different video you uploaded, you mention to ditch the underscore. Great video tho!
Out of actual curiosity: You wouldn't EVER create a method that is called "AddEnemy" which contains a single method call, which incidentally is called by "_enemies.Add()", would you? For me, THAT's the definition of spaghetti code - dogmatically "making things more readable" (except in this case LMFAO) by trading in tons of performance when all of these "simplifications" are added up. Same thing goes for creating a reference to a reference (initializing a variable) just to use it in some conditional statement, apparently for readability. I don't get it. Am I too intelligent? :D WhY dO pEoPlE dO tHiS?
Well, I don't really know how Unity or C# handles references(in terms of memory leaks) but creating a key, value pair(if that's what you're referring to) in any language is a given and the key not being a primitive is okay. The performance of retrieving items does depend on how the search algorithm is implemented for the key's type, though.
For those looking for the next cool thing to pick up with Dictionaries. Look at using a Dictionary with a Command pattern. and you can pass in an Action by type or Type and it will return a method you can then fire. This is how you can make quickly variable Command patterns that are manageable versus monster smelley Switch trees.
Arrays, lists, hashsets, dictionaries, stacks, queues... Nice video. There's so many ways to deal with data it's good to have the correct tool for the job. I wouldn't want to use a sledge hammer on a finishing nail.
Fixed the audio, thx josh for fixing it and everyone else for calling it out :)
Unity3d College anytime!
so much better!!!! gratz
@@DemaGraphexTube Hi I'm a noob myself but I believe PlayerPrefs only accepts floats, ints, and bools by default. I created a class that converts a list into serialized playerpref entries and another class that converts the entries back into a list, i'm sure you can do something similar for arrays
@@DemaGraphexTube sure thing bud are you on discord? grapemane#0012
@@DemaGraphexTube yes. in the same class i have
public void Save()
{
/// --------------------- SKILLS
SaveFloatList(PlayerController.instance.XP, "SkillXP", 50);
SaveFloatList(PlayerController.instance.level, "SkillLevel", 50);
/// --------------------- CURRENCY
SaveFloatList(PlayerController.instance.prestigePoints, "PrestigePoints", 50);
SaveIntList(PlayerController.instance.itemsEaten, "ItemsEaten", 50);
SaveFloatList(PlayerController.instance.chickensEaten, "ChickensEaten", 8);
SaveFloatList(PlayerController.instance.currencyOwned, "CurrencyOwned", 50);
SaveFloatList(PlayerController.instance.upgradesOwned, "UpgradesOwned", 50);
}
and
private void LoadAll()
{
/// --------------------- SKILLS
LoadFloatList(PlayerController.instance.XP, "SkillXP", 50);
LoadFloatList(PlayerController.instance.level, "SkillLevel", 50);
/// --------------------- CURRENCY
LoadFloatList(PlayerController.instance.prestigePoints, "PrestigePoints", 50);
LoadIntList(PlayerController.instance.itemsEaten, "ItemsEaten", 50);
LoadFloatList(PlayerController.instance.chickensEaten, "ChickensEaten", 8);
LoadFloatList(PlayerController.instance.currencyOwned, "CurrencyOwned", 50);
LoadFloatList(PlayerController.instance.upgradesOwned, "UpgradesOwned", 50);
}
and I call those 2 functions when the player clicks save or load
REAL WORLD EXAMPLES!!! Music to my ears man, and something that would just be sarcasm out of so many 'build a game in 10 minutes!!!' youtubers.
I followed your instructions... my mom just published her first game!
Queue was used in Unity Beginners Code Kit to limit audioSources to play as I understand. They have a singleton that instantiates limit number of prefab audioSources for particular group of sounds for example player. And use queue to remove first audioSource in queue and add it to end of the same queue when new sound is going to be to played.
Jason you have THE BEST unity tutorial content on the internet
This topic is too complicated for me, for now. But i watched it with greate interest, and save for later - i will defenetly watch it at least one more time, when I'll became more familiar with collections in C#.
Thank You for this video, and all videos, You have done, they are greate! Very interesting, informative, useful and well done.
watching any video from this channel is not a waste of time, no matter how experienced you are. Keep the great working!
Another very useful video! I think it's great that you prepare everything and don't waste time typing line by line.
Ideas for future episodes: serialization (lists and dictionaries) and coordinates (world, local, calculations and conversion of mouse pointer)
Nice explanation.
So a HashSet is basically a List with unique items only. [Add, Remove].
Dictionary, self-explanatory. [Add, Remove].
Stack is LIFO (Last in first out) - [Push Pop].
Queue is FIFO. [Enqueue, Dequeue].
Man I accidentally had playback at .75 speed. I thought you were drunk AF.
Lmao
only way to code
Awesome! The best explanation of using collections! Thanks!
Hashset is actually what I need in my project lol, why havn't I heard of it before is beyond me.
Great video mate, nice simple use case for how to use hash sets. Cheers
Relative performance matters as well. Searching a large list for an element is slower than a hash set or a dictionary, but accessing an element in an array is crazy fast by comparison. If you can use an array or list where the key is just the array index, performance will be much, much faster than a dictionary.
In my experience with Unity and C# in general, the list has to get pretty big before a Dictionary starts to outperform it. Like a couple dozen items or so.
Thank you! This is a very useful video. Coming from a PHP background, the different types of collections in C# had me a bit puzzled, and this cleared everything up.
Glad to hear it was helpful. Collections are huge and really confused me for the first couple years. :)
Very helpful, as always. Thanks Jason!
Super useful stuff, thanks Jason.
Initially the idea of TH-cam premiers seemed silly and pointless to me, but I stumbled into yours (for this video) and I must admit it was awesome seeing you there in the chatroom while you also talked us through in your video... it's like you finally hired an assistant. = )
silly question but how do i get to see that?
@@teemuleppa3347 There's notifications that a that a premier video is scheduled in advance if you want to watch it live. For not-live ones you just gotta noodle around the video's page until you find the Replay Chat button. It even scrolls the chat history while you're playing the video.
Audio sounds much better than before but you still reduce the harshness by making a dip in the frequencies around 1000 - 2000 Hz. WIll make your voice a lot smoother.
I'll give that atry, thx!,
Ive been a self though developer for more than 5 years... until this this day I had no idea what a hash set was... allways checking list to see if an item with a custom id was already there... oh silly me.... so yeah, thank you a lot.
Your channel will grow if you keep up the excellent work!
The main reason/situation when I use HasSet instead of List is when i need to frequently check what's inside it. List.Contains() is an O(n) operation (that is, it takes longer the more stuff there is in the list), while HashSet.Contains is O(1), so it's constant speed no matter what. Dictionary.ContainsKey() is O(1) as well, but if you don't need to pair keys and values it's needless hassle.
Thank you for fixing the audio please use a dark script editor I can see the code
Great stuff, as always!
Thanks for sharing.
But I didn't quite get the use case for the temporary stack of items. Usually you just won't allow addition of new items to a full inventory instead of keeping them in a temporary place that you have to make a UI for.
It is useful if inventory is meant to hold stackable items. Inscope studios on youtube made set of tutorials on system like that with good explanation why.
@@dfhdgsdcrthfjghktygerte I'm not sure that I was clear enough. I'm talking about the "_overflowItems" collection.
If this is a collection of items that didn't get into the actual inventory, where would they go in real scenario?
@@MTandi I don't want to bring extra confusion, i just briefly looked into his code but to me it seems the purpose of _overflowItems is not to keep extra items, but add the ability to pop 1 item and push another one in it's place in case collection is full. And it looks like the popped item is removed in the process but it can be put into different slot, bag, inventory page for example.
It's a system we have in pantheonmmo.com where extra items you get from quests/etc that are force added to your inventory go into an overflow, then when you free a slot you can put it in your actual inventory. The goal is to allow people to finish quests/etc and still get the items without having to clear a spot in the inventory or stop the progression until they do :)
Good job! Keep posting.
Good stuff.
Stack we can use for the Back Button functionality.
Interested into see some info on Link Extension method and Link Statements
Thanks for awesome practical video tutorial. I noticed that in editor you have hints like "2 usages", "frequently called" etc What's that and how do you get them displayed? Thanks and like
p.s. I also like how you hide setter of instance property, nice touch!
p.p.s. Whenever you use fade out effect, I am thinking that my iPad went to sleep due to long inactivity :D
Nice video! Keep a good work!
Awesome, thank you for the lecture
A good beginner tutorial.
But there are some missing elements from the info you provided and that keeps it at a beginner level. (Just trying to provide constructive criticism)
What you may or may have not known:
Lists are just fancy arrays. And for your inventory system it would have been better because dictionaries have a cost to calculate the hashcode of an object. Which isn't even relyable unless you implement it yourself or don't create duplicates.
For inventory the tldr is keep an array.
HashSets & Dictionaries (or hashmaps) use hashcodes to define the uniqueness with a checkup for doublechecking.
Also when iterated over it the output is semi random (foreach) because its based on how the hashcodes got sorted. Also in you lose performance because you iterate over every entry even if that is not present.
There are faster options for hashsets/maps that are linked that give greater iteration performance but they are not by default in c#.
Also there are two types of queue. A stack that is first insert & last extract.
Or a linked list which is first insert & first extract.
Also sorted queues like treesets or priority queues that can sort the values based on a comparator.
Also what you may should suggest later too is to mix them up because loads of developers in unity I have seen decide to use only 1. You kinda did that with your dictionary stack but still wanted to tell how important that is.
Thanks for providing a good beginner level tutorial because they are not as present. ^^
I hope this does not sound as yelling because I just wanted to provide Info's that back then I wished I had known.
plz make a c# game development toturial
super helpful, thanks!
how is that a singleton if you dont check for other instances before assigning "this"
Maybe I am wrong, but if you are sure that this is going to be the only instance of this class and there is only one scene you can do that this way.
@@fastbyte2235 its not a singleton pattern then, a singleton is checking to be sure its the only one
People just got used to call any static field/property that returns an instance of that object a singleton, but if you don't check/destroy duplicates it really isn't a true singleton. But still, most people don't like the if (instance != this) { Destroy(this) } pattern because you lose control of what's being destroyed unless you are careful. But of course, if you don't trust yourself or your team to be careful with it you just shouldn't mess with the destroy singleton pattern at all
I want create vocabulary game for teach languages. For me best collections for words is dictionaries ?
Slightly more advanced, but k-d trees are also interesting ...
How would I save the dictionaries to xml or any other way of parsing?
Your the best
Yeah, I knew about these beforehand but I normally just default to lists/arrays because of the ease of use.
Thank you
Please use dark theme in IDE
ew, no thanks. Dark theme is brutal on the eye strain.
@@BobrLovr The whole point of dark theme is to reduce eye strain. Looking at a bright screen is bad for your eyes.
I’m the guy that uses arraylists far too often. I like dictionaries and queue though too.
I think after watching this most of the places I use arraylists I might use hashmaps just because o check for redundancies far too often.
Thanks!
When you used the dictionary as an example, in the method LoadItems(), in the head of ToDictionary(key and value) the variables before the arrow, =>, are defined nowhere locally nor as class variables, does that mean key and value can be used after the method call of ToDictionary, or does it have something to do with ToDictionary?
So, just a question (it's not related to this video tho), the underscore for private parameters is recommended? Because I saw in a different video you uploaded, you mention to ditch the underscore. Great video tho!
Just got into C# and Unity, almost instantly moved from arrays to lists.
1000th like! whoop whoop
Too bad most of these don't get serialised by unity
Honest suggestion for your future videos - please stop shouting into your mic. It is VERY hard to go along when you are being this aggressive.
He's not shouting. The mic is pretty bad and peaking at the mids so when he does raise his voice slightly it clips which causes the crackling.
Out of actual curiosity:
You wouldn't EVER create a method that is called "AddEnemy" which contains a single method call, which incidentally is called by "_enemies.Add()", would you?
For me, THAT's the definition of spaghetti code - dogmatically "making things more readable" (except in this case LMFAO) by trading in tons of performance when all of these "simplifications" are added up.
Same thing goes for creating a reference to a reference (initializing a variable) just to use it in some conditional statement, apparently for readability. I don't get it. Am I too intelligent? :D WhY dO pEoPlE dO tHiS?
Well, I don't really know how Unity or C# handles references(in terms of memory leaks) but creating a key, value pair(if that's what you're referring to) in any language is a given and the key not being a primitive is okay. The performance of retrieving items does depend on how the search algorithm is implemented for the key's type, though.