Bullet won't duplicate correctly

Godot Version

4.5.1.stable

Question

hi, i was trying to make my enemy shoot bullets by picking them trough an array and shooting them, like this

var enemyProjectiles: Array[Projectile]

func getSelectedProjectile(selectedProjectile: int) -> Projectile:
	return enemyProjectiles[selectedProjectile].duplicate()

because of a dynamic bullet creation system i have in place i create the bullets once the enemy is ready and then have them stored in the array enemyProjectiles, this should speed up the shooting significantly since the enemy isn't creating new bullets constantly, however when the enemy bullet hits something it gives me this error

Invalid assignment of property or key ‘varName’ with value of type ‘bool’ on a base object of type ‘previously freed’.

it would take very long to explain what the bool does, but basicaly it checks if a special proprety of the bullet is true and then applies some special effects to it based on information passed during the construction done previously

i have never encountered the previously freed type before, so i'm at a loss here, i though the problem was that the enemy was shooting the same instance of the bullet every time, but even with .duplicate() it still doesn't work

i can share more of the code if needed

Your bullet gets freed, and you try to reuse it. My guess is that your collision handler frees the bullet upon impact.

shouldn’t the duplicate function pass a copy of the bullet?

fixed it, turns out the problem wasn’t with the projectiles but with the special effects i applied not being duplicated

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.