Spawning ~100 projectiles at once

Hello there, I’m trying to work on shotgun mechanic where I spawn ~100 projectiles all at once, and I’m looking for some ideas on performance. Until now I’ve just been spawning the one projectile but now, I’m spawning anywhere between 1 and 100. I’ve changed my code to spawn 100 at once, and it seems like I’m having some problems. First of all, whenever I spawn 100 actors it stutters for a second. I’m guessing that’s because it doesn’t have the chance to finish the tick function. Second of all, when I replicate this it seems to cause some serious network lag. This is not entirely surprising, that’s a lot to replicate all at once.

If you take a look at the screenshot it gives a much better idea

I’ve tried a couple things. I’ve tried separating them out into components. The problem is that I have properties in blueprint, which can’t be determined in the constructor to initialize the components! I’ve tried disabling replication and spawning client/server versions. I’m not fond of this idea, and it did sort of work, but it still causes a huge hiccup when they all spawn at once.

I’m looking for some idea as to how I can possible to it so that they spawn without the crazy lag. Any help is appreciated. Thank you!

Can you fake things a bit?

Eg, instead of spawning 100 projectiles, do traces instead. Then do 1 particle effect with a random number of projectiles. Lots less replication.
If you need to see impact locations on the client, use a shared seed for your randomizer on the spread, and reproduce your traces on the client. The client can then produce impact effects - 1 replication call.

The fake here is that the projectiles won’t be an exact match for where the traces went, but the impact points will.

This is how I did it and it removed the lag.

EDIT: I’ve just looked at the code (its a year or so old now), and I actually create individual particle effects for each trace on the client, so my effects do reflect the traces on the server and its pretty lag free (probably because it doesnt have to spin up a full actor and replicate).

As GeekyPayBack here said…fake it.

Spawning 100 projectiles in a frame isn’t really desirable, or practical. You could probably do some things to help performance like having the projectiles spawned in advance and moved under the level, and move them into place instead of ‘spawning’ them, but that doesn’t solve the replication issue.

Really though…it’s highly unlikely that actually spawning and modelling 100 projectiles is going to provide any real value to the player experience. You could probably spawn just 10 and fake the look and behaviour with some effects/particles.

yeah, just to comment again. I create 10/15 particles in my code. If I were going above 20 I’d definitely be faking it as at that point there is so much going on the user wouldn’t know that what it saw and what the server did were different things.

That’s a pretty good idea, but I’m not entirely sure it will work in my scenario. The problem is that the game I’m creating is more slow moving bullet physics, so this isn’t like a regular type of shotgun. The bullets you see there move pretty slowly, maybe twice that of the max default player speed. Like you say if I did a trace and figured out where all of them hit all at once, then sure, that could work. But that doesn’t fit my game style unfortunately. Take a look at the video below to get a better idea of how a shotgun usually would work:

[video]Shotgun preview - Infantry - YouTube

Edit: Nevermind, I think I understand. I’m going to give this a shot.

The pellets in your video are uniformly spaced and very predictable but are slow moving. Because of this I think you do need to spawn individual projectiles.

Make sure the projectiles don’t replicate and have the client produce its own copy of them. If you are still lagging have each weapon have a pool of pre-spawned projectiles that are hidden and not collidable, then make them visible for the time you need them, then pooled again when they impact.

Okay, I’ll give that a shot. I tried that before but yeah, spawning 91 of them was causing a hiccup when you do that in one tick. Pooling sounds like a good idea, thanks!

Is lag something you’re just going to have to deal with in this situation I’m guessing? As in the client and server will be off by whatever their latency is.

This is just a wierd idea, but : If shooting 100 projectiles in one frame is too much, pre-spawned projectiles isn’t good but you absolutely need to have 100 individual projectiles to be shot, how about changing your shoot mechanic so instead, when using the “shoot” button, it’s shooting 10 projectiles per frame for the next 10 frames? Or shoot 20 projectiles per frame for the next 5 frames?
5 frames isn’t that noticeable during 60+ fps gameplay (less than 10th of a second) and it’d give your “shotgun”-like shot a more realistic look and some variety since even with realistic shotguns not all bullets exit the gun at the exact same time.

If you want to counter this so bullets end up at the max bullet distance at the same time you could give the bullets that get spawned later a slightly higher speed (set bullet speed = normal bullet speed + small bonus speed depending how late that bullet got spawned) so they arrive at the max distance at the same time with the bullets that got shot on the first frame.

Yeah was starting to look at pooling and it certainly has its caveats. I could do that, but honestly the game I’m making is a replica of an old one and I’m trying to get the mechanic so that they are identical. I might be able to spawn them, over ~30 frames, hidden, inactive, then once they’re all spawned, activate them and show them.