Pooling and varying stats (Mobile)

Hi :slight_smile:
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”.
  • ???

Hope I’m somewhat clear :smile: cheers

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.

1 Like

Thanks for the reply Csofranz :slight_smile:
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 ^^

-Cheers

…
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.

1 Like

@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.

  • Cheers :slight_smile:
1 Like