Object Pool and components
Hi, I am using a simple object pool for use with drawing generic objects such as lines, rectangle, circles, curves, etc. Sometimes I add other components to them such as BoxCollider2D, FollowMouse, etc. The question I have, what would be the best way of handling it?
Right now I add the components when I need them then destroy them when I put the object back into the inactive list but after coding this I thought, this kind of defeats the purpose of an object pool if I am just going to create and destroy the components but not the game objects.
What would be the best way to eliminate the need to destroy components after creation. Also, it is not viable to create a pool for each type of object because sometimes I add components outside of the initial script the pool is in so I cannot think of every combination of components that I might need.
Thanks in advance for any help!
Which method did you use at that time!? I have the same problem with you. Thank you.
Answer by pekalicious · Nov 16, 2015 at 12:13 AM
True, there is no purpose for you to use an object pool if you are going to be destroying them. Typically an object pool has a Create method that initializes a component's variables and a Release method that disables the game object and/or component and resets its position to a default location.
But the question is, why do you think you need an object pool for everything? Are you going to be creating different components that each has more than 1000 objects in the scene?
Remember, an object pool is for pooling a single "type" of entity. I.e. if you have 1000 bullets, then you have one bullet prefab and an object pool that controls those prefabs (Creates and Releases when appropriate). That bullet prefab can have any number of components. The object pool will make sure to config each component (based on your parameters) when creating.
Now, for something like a player game object, or even an enemy game object, that you will most likely only have a few in the scene, there's no need for an object pool.
I understand what your saying and without going into too much detail I'm making a reusable primitive drawer which will be used for drawing a map section so there will be a lot being created and destroyed. This map is also editable hence the nerd for different components.
I am just tryingto see if there is a similar method for components so I can add them if needed but not destroy them afterwards but are able to be reused if needed.
When you say "drawing a map section", are you talking at runtime? As in, the player will be able to draw and edit a map while he's playing the game? And he will also be able to attach components to different sections of the map? If so, then, as far as I know, I don't think there is a way to remove a component without destroying it. The only (ugly) way I can think of is having a prefab game object for each component, and adding the game object as a child to the map section. That way you can disable the game object and reuse it and you will only need one object pool for each type of component, but all of them will be prefabs, so the code is the same.
Yes that's what I was talking about and thanks for your time, since there is no way of doing what I was wanting to do I went a different rout and wrote a static class that will run delegates on game objects, it can be created once and reused, which is what I was ai$$anonymous$$g for. Even though you didn't suggest it you allowed me to move towards the right direction and have my code running as expected, thanks for the nudge!
BTW the map is too large to be creating everything then just moving the camera around so I wanted to be able to load the information in view then just draw those without continuiously creating a destroying objects. Sorry for not being too clear on that just figured it wasn't relevant to the question.
Your answer
Follow this Question
Related Questions
Destroying object multiple times. 0 Answers
If you want to destroy the game object, please call 'Destroy' on the game object instead. 1 Answer
Is there a way to refer to the only script the object has without naming the script? 2 Answers
Keep GameObject destroyed on returning to the scene 1 Answer
Make an object move from Point A to Point B then back to Point A, and then destroy itself 1 Answer