Resolved Preventing new projectiles from spawning from new projectiles

POCKETS

Terrarian
OK, so I know the title is a bit confusing, so I'll explain the issue -

I have a projectile that needs to spawn a new projectile (same projectile as the original) after a certain amount of time.

Currently, I'm using projectile.timeLeft to accomplish this inside my projectile's AI.

However, the new projectiles are also spawning new projectiles (obviously), so I need a way to prevent this from happening.

I just need new projectiles to spawn once after the original projectiles have lived for a certain amount of time.

Is there a better place to accomplish this task than inside the AI? It makes sense that they'd keep spawning, as new projectiles are using the same AI...

EDIT: I suppose if I made the new projectiles a completely separate projectile, and not the same exact projectile, it might work better...
 
OK, so I know the title is a bit confusing, so I'll explain the issue -

I have a projectile that needs to spawn a new projectile after a certain amount of time.

Currently, I'm using projectile.timeLeft to accomplish this inside my projectile's AI.

However, the new projectiles are also spawning new projectiles (obviously), so I need a way to prevent this from happening.

I just need new projectiles to spawn once after the original projectiles have lived for a certain amount of time.

Is there a better place to accomplish this task than inside the AI? It makes sense that they'd keep spawning, as new projectiles are using the same AI...
If you're not using the ai array yet, then this would be an ideal use of it.

In your AI, add an if-statement around your projectile spawn code.
C#:
if (projectile.ai[0] != -1)
{
    // Spawn stuff
}

Then the only variation is in spawning the projectiles:

When you spawn the main projectile, NewProjectile should look like this (if you're spawning projectiles via item.shoot then you don't have to do anything):
C#:
Projectile.NewProjectile(position, velocity, type, damage, knockback); // Normal stuff
When you spawn the child projectile, NewProjectile should look like this:
C#:
Projectile.NewProjectile(position, velocity, type, damage, knockback, projectile.owner, -1); // We added two arguments.
 
If you're not using the ai array yet, then this would be an ideal use of it.

This is a subject I know nothing about, and it's probably time I looked into it.

Unfortunately, both projectile.ai[0] and projectile.ai[1] are being used in the projectile AI as part of the intricate homing behavior. I don't know enough about them to know whether or not I can still use them. I do know they are synced automatically, so they are multiplayer compatible, but that's about it.

I will do some testing with this, and if it turns out that I can't use either of them, then I'll just have to create a separate projectile class, and use that.

EDIT: Does seem to work just fine ;-)

AH, ok I see how this works now.

The if statement is checking to make sure that projectile.ai[0] does not equal -1. If it doesn't, the statement is true, and the new projectile is spawned. However, as soon as that new projectile is spawned, I'm assuming that projectile.ai[0] is assigned the -1 value, so at that point, the if statement will always be false, meaning it never runs again.

I'm not sure about the assigning value portion, but logically this code does make sense to me now.

Cheers!
 
Last edited:
Back
Top Bottom