tag:blogger.com,1999:blog-2883262736964699364.post6783760678079644283..comments2020-11-25T01:46:56.021-05:00Comments on Advice From The Swamp: A Generic Pool Collection Classthomas h aylesworthhttp://www.blogger.com/profile/14202055419168981709noreply@blogger.comBlogger17125tag:blogger.com,1999:blog-2883262736964699364.post-1243949134558451312011-12-16T16:51:00.999-05:002011-12-16T16:51:00.999-05:00I have a question, if I have 3 types of enemies (F...I have a question, if I have 3 types of enemies (FastEnemy, NormalEnemy and SlowEnemy) that inherits from a base class Enemy, can I create only one repository o should I create 3 repository, one for each type of enemy?<br /><br />THANKS!Hernan Zaldivarhttps://www.blogger.com/profile/03894581211300599578noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-62243767753297945282011-12-16T15:45:57.923-05:002011-12-16T15:45:57.923-05:00Great solution Thomas!! download links seem to be ...Great solution Thomas!! download links seem to be broken, can u upload them to somewhere else please?<br /><br />THANKS! greetings from Argentina<br />HERNANHernan Zaldivarhttps://www.blogger.com/profile/03894581211300599578noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-81949680790316003492010-08-18T20:21:51.909-04:002010-08-18T20:21:51.909-04:00Because the Node type used by my Pool class is a v...Because the Node type used by my Pool class is a value type, neither the use of a Dictionary nor the foreach enumerator create any excess garbage. I have verified this using the CLR Profiler, and have used this class in games that iterate through thousands of objects per frame without any problems with garbage collection.<br /><br />For some good articles on how to use collections and foreach thomas h aylesworthhttps://www.blogger.com/profile/14202055419168981709noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-64923958929644972212010-08-07T07:49:00.005-04:002010-08-07T07:49:00.005-04:00Using enumerators, generated by foreach causes all...Using enumerators, generated by foreach causes allocations at run-time --> problems with GC --> you shouldn't use itAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-14585132161622807852010-08-06T03:56:39.164-04:002010-08-06T03:56:39.164-04:00Dictionaries can create a lot of garbage, be caref...Dictionaries can create a lot of garbage, be carefulAnonymoushttps://www.blogger.com/profile/08159372014989024070noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-60022640059398487622008-06-24T20:57:00.000-04:002008-06-24T20:57:00.000-04:00To avoid the problem with structs returning a copy...To avoid the problem with structs returning a copy, couldn't you use a dictionary in place of the node system? It would require replacing "pool" with a Dictionary<int,T> and "active" with a Dictionary<int,bool>, and using GetHashCode for objects in order to index them. The class retains its generic quality and decoupling from the objects it is managing, but also removes the ugliness Anonymousnoreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-42608717977850622712008-01-16T16:59:00.000-05:002008-01-16T16:59:00.000-05:00your code is great but I missing the part of Multi...your code is great but I missing the part of Multi Thread awareness - or do I missunderstand here something?roni schuetzhttps://www.blogger.com/profile/07513115936020791200noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-36431022891437890932007-09-23T23:21:00.000-04:002007-09-23T23:21:00.000-04:00Thanks for the comments, IronMoped. I'm glad you ...Thanks for the comments, IronMoped. I'm glad you find the class useful!<BR/><BR/>The reason for initializing the pool items in the constructor is that time spent during initialization isn't as important as time spent in the game loop. For the same reason, it's preferable to dynamically allocate everything during initialization instead of during the game loop.thomas h aylesworthhttps://www.blogger.com/profile/14202055419168981709noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-56873977978598978412007-09-19T15:39:00.000-04:002007-09-19T15:39:00.000-04:00Alright, last comment, I promise. In the snippet a...Alright, last comment, I promise. In the snippet above, it should be "new T()", not "default(T)".Mopedhttps://www.blogger.com/profile/04721881758109447106noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-47757710726742665672007-09-19T15:10:00.000-04:002007-09-19T15:10:00.000-04:00Your code is great; it's very readable and well th...Your code is great; it's very readable and well thought out. Have you thought about default-constructing items on demand, rather than in the constructor? It'll rid your constructor of the loop. I extended your code a little:<BR/><BR/> lock(sync)<BR/> {<BR/> if(active.Count == capacity)<BR/> {<BR/> throw new Mopedhttps://www.blogger.com/profile/04721881758109447106noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-20156303621745033572007-09-19T14:43:00.000-04:002007-09-19T14:43:00.000-04:00Nevermind. :)Nevermind. :)Mopedhttps://www.blogger.com/profile/04721881758109447106noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-9925113227148958912007-09-19T14:38:00.000-04:002007-09-19T14:38:00.000-04:00I don't think this implementation will work with v...I don't think this implementation will work with value types.Mopedhttps://www.blogger.com/profile/04721881758109447106noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-24768127582387833342007-06-18T21:23:00.000-04:002007-06-18T21:23:00.000-04:00Whoops! Thanks for pointing that out. It should ...Whoops! Thanks for pointing that out. It should be fixed now.thomas h aylesworthhttps://www.blogger.com/profile/14202055419168981709noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-85657162698087032862007-06-18T16:58:00.000-04:002007-06-18T16:58:00.000-04:00the source is fine. he just means on the web page ...the source is fine. he just means on the web page it's missing all of the < and >'s so the creation of the Pool didn't look like it was a generic. I was confused by that at first as well. Thanks for sharing :DAnonymousnoreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-2591129519268266122007-06-11T14:22:00.000-04:002007-06-11T14:22:00.000-04:00Thanks for the comments, Andrew. But I'm confused...Thanks for the comments, Andrew. But I'm confused by your statement about "the generics have been lost." Did you download the source?<BR/><BR/><A HREF="http://members.cox.net/thomas.aylesworth/Swamp/Pool.cs" REL="nofollow">Pool.cs</A><BR/><BR/>If you're having problems downloading that, or if you can be more specific about the problem you are having, please let me know!<BR/><BR/>Thanks,<BR/><BRthomas h aylesworthhttps://www.blogger.com/profile/14202055419168981709noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-5012488220260554622007-06-10T21:19:00.000-04:002007-06-10T21:19:00.000-04:00Dude.. it seems the html is eating up your code a ...Dude.. it seems the html is eating up your code a bit.. All the generics have been lost :( At first I was confused and thought perhaps you just had untested code ;)Andrewhttps://www.blogger.com/profile/11666771405287620018noreply@blogger.comtag:blogger.com,1999:blog-2883262736964699364.post-48219481163910826792007-06-06T20:36:00.000-04:002007-06-06T20:36:00.000-04:00Very very nice article!! Yeah, pools are great! ...Very very nice article!! Yeah, pools are great! My pooling system isn't as efficient as yours. You certainly gave me some ideas. ThanksAndrewhttps://www.blogger.com/profile/11666771405287620018noreply@blogger.com