Hi
So, I was wondering how people work around both using an object pool while also requiring some of these items to be changed while in use.
E.g. my enemies use bullets which they fetch from pool, but in certain cases (like when theyâre powered up) their bullets do more damage. Right now Iâm considering âŚ
Using âGetComponentâ to access projectile class and update values to match character (pooler return reference to gameobject, not the class). Multiple enemies might be using the same kind of projectile, but have different damage stats. This isnt a bullet hell shooter, but Iâd assume using âGetComponentâ every time to be bad?
Try to make the pooler more generic, allowing returning of class reference so no âGetComponentâ is required.
Make projectiles local to the enemy (basically giving them a pool each) and avoid main pooler. As theyâre local, they really only need to be changed on âpowerupsâ.
GetComponent() isnât bad by itself, itâs only considered bad if you invoke it many times for the same object for the same reason (e.g. in Update()). In those cases caching the component is preferrable.
In your specific case, I think there are only two points in time when you need to GetComponent: when the projectile is fetched from the pool, and if/when it actually hits something.
When you design something like that (projectiles with multiple Versions), a simple setup is using multiple damage âkindsâ (physical, electrical, Magic, etc) with float variables indicating the amount. Then write a general
public Enum projectileKind {physical, electric, acid, entropy, magic};
public void initializeProjectile(projectileKind damageType) {}
method that you invoke directly after fetching from the pool. It sets up all damage modifiers depending on the projectile Kind you want, right after GetComponent(). If/When the projectile hits, GetComponent again, and apply all damage through a unifies ApplyDamage() method.
Thanks for the reply Csofranz
I was simply wondering if someone had a different approach to this. While fighting groups of enemies the pooler is going to be fairly busy, and fetching component each time just seemed like a bad design.
I guess the only other way I can see would be splitting the pooler up, allowing it to contain a referene to the class rather than the gameobject. Its just not very flexible.
Perhaps I just need to get over my performance fears around getComponent ^^
âŚ
4. Use the OnDisable function (or a custom OnPooled method call) to reset the state of bullets whenever they are pooled.
Options 2 and 3 (generic pool, swapping local pools) both sound like smart and elegant solutions. Even if there isnât that much of a performance improvement, both of these idea could also just result in more succinct and readable code.
That being said, just using GetComponent most likely wonât have a noticeable impact on your performance. You can use the Profiler to do a quick test to see just how much of an impact this would have.
@SisusCo - Resetting their own state would definitely help, atleast in cases where changes donât occur often. Will try and profile, but like yaâll said Iâm probably making a fuss over nothing.