Hey there! Apologies on the delay in my response - hopefully this explanation helps. A Dictionary is set up which includes 2 properties, a KEY and a VALUE - think of the KEY as almost like a username, and the value is the account that it belongs to. When you put the Key into the dictionary, if it contains an entry (as in the account exists somewhere), then it will return it. So setup would be like: Dictionary myDictionary = new Dictionary(); in this case, our KEY is the string and our VALUE is the integer. Now think of a list setup, you would add to a dictionary similar to how you would a list, but this time you need the identifier (key) and you need the value. So initialising the dictionary would be something like: myDictionary.Add("Garnet", 100); so by doing this, we now have a unique entry for something called Garnet that has a value of 100. When we want to look through the dictionary to find a value, we can search by doing something like: myDictionary["Garnet"] -> but this is ASSUMING we know that there's a key for Garnet, a more common approach would to null check it if(myDictionary.ContainsKey("Garnet")){ print(myDictionary["Garnet"]); } It's definitely a lot to wrap your head around, but I hope this helped in some way with it!
For some reason when the pool is full and all of them are in use and then starts instantiating new objects, it then skips generating every 2nd object from then on.
If anyone else has this problem changing the last line of the *EnqueueNewInstance()* method fixes it: Instead of return newInstance; write this return (T)poolDictionary[key].Dequeue();
really cool explanation, very inspiring! Thank you!
So useful thanks so much! Hadn't seen this before. So well explained.
Would love to see more tutorials :)
Appreciate it! More tutorials coming :)
Amazing Explanation
Appreciate it! Glad it was helpful :)
By now, there is already Unity built-in object pooling api :)
Im having a really hard time understanding how dictionaries work :(
Hey there! Apologies on the delay in my response - hopefully this explanation helps.
A Dictionary is set up which includes 2 properties, a KEY and a VALUE - think of the KEY as almost like a username, and the value is the account that it belongs to. When you put the Key into the dictionary, if it contains an entry (as in the account exists somewhere), then it will return it.
So setup would be like:
Dictionary myDictionary = new Dictionary();
in this case, our KEY is the string and our VALUE is the integer.
Now think of a list setup, you would add to a dictionary similar to how you would a list, but this time you need the identifier (key) and you need the value. So initialising the dictionary would be something like:
myDictionary.Add("Garnet", 100);
so by doing this, we now have a unique entry for something called Garnet that has a value of 100.
When we want to look through the dictionary to find a value, we can search by doing something like:
myDictionary["Garnet"] -> but this is ASSUMING we know that there's a key for Garnet, a more common approach would to null check it
if(myDictionary.ContainsKey("Garnet")){
print(myDictionary["Garnet"]);
}
It's definitely a lot to wrap your head around, but I hope this helped in some way with it!
I get the concept but literally zero idea how the code works, but my fps has never been so good.
For some reason when the pool is full and all of them are in use and then starts instantiating new objects, it then skips generating every 2nd object from then on.
If anyone else has this problem changing the last line of the *EnqueueNewInstance()* method fixes it:
Instead of
return newInstance;
write this
return (T)poolDictionary[key].Dequeue();
Why use queue and not stack?