RhenaudTheLukark / CreateYourFrisk

Rhenaud The Lukark's Unitale fork
GNU General Public License v3.0
135 stars 57 forks source link

[Issue] Removing a bullet doesn't remove its sprite #29

Closed TomaszewskiPatryk closed 5 years ago

TomaszewskiPatryk commented 5 years ago

Describe the bug Using bullet.sprite.isactive return true, even after bullet.Remove(). I'm not sure if that means that the sprite is still active or if this flag just didn't change, but either way, it should return false. It's especially important when you loop through sprites, out of which some are just regular ones and some are "attached" to a bullet.

Code To Reproduce

bullet = CreateProjectileAbs("bullet", 70, 100)
bullet.Remove()
DEBUG(bullet.sprite.isactive)

Setup:

Eir-nya commented 5 years ago

Bullets and sprites will always stay in memory so long as a reference to them exists. In your example, that would be the variable bullet. The best way to avoid the build-up of sprites, bullets and text objects is to set their variables to nil after removing them.

For example:

bullet = CreateProjectileAbs("bullet", 320, 240)
bullet.Remove()
bullet = nil
RhenaudTheLukark commented 5 years ago

A bullet's sprite can't be disabled because a bullet is actually not a "bullet": it's an object which is part of a pool, and this pool of objects act like a bullet pool.

Once a bullet is removed, this object is sent back to the pool so it can be reused later.

Removing a bullet's sprite would literally remove the sprite component related to the bullet's object, which would prevent us from using it again in this pool.

So no, there won't be any fix about this anytime soon. You can access a bullet's sprite using bullet.sprite, so use it, and don't store a bullet's sprite.

If you want to check if this bullet's sprite is still active, check if the bullet is active instead.