Hey, I’m working on an open world sort of game and there will be possibly 30-60 sentry guns around the map. I’m trying to figure out the most optimized approach to managing all these turrets.
-
What’s the best way to constantly check for nearby players for each sentry gun? My current solution is just a ‘while’ loop with a one second iteration that pauses for 10 seconds if there are no players even remotely close.
-
Is it more efficient to use only one script to handle all of these sentry guns or one script for each gun?
-
Feel free to provide any other pieces of advice on dealing with this situation.
2 Likes
Is there a significant performance gap between a single script and multiple?
1 Like
Depends.
When I had ~100 zombies at once in my game, each with their own script, and checking every 1/30th of a second for a target, the total of the scripts’ memory use was about 25%.
Changing it to check every 2.5 seconds and controlling all of the zombies with one script made the memory use drop significantly to 2%
2 Likes
But the first implementation was checking every 1/30th of a second while the other was checking only once every 2.5 seconds. That could have been the cause for the drop in memory use rather than using only one script.
1 Like
I tested both. Running every 2.5 seconds with a script in every zombie was about 12% usage, while checking every 1/30 of a second with one main script used about 16%
2 Likes
on top of a higher wait period between each check, you could use a region3 maybe or other methods of getting a less accurate, but smaller count of targets to pick from and do the other checks, if you either have checks that are resource or time intensive or if you have tons of possible targets, and you could use doing checks on fewer entities
2 Likes
I don’t imagine you’ll have any major issues doing it how you are with some distance checks on a loop.
Doing all of this within one script is definitely a good idea - actual performance difference probably isn’t going to be a realistic concern. The real advantage is organisation through centralisation.
Wouldn’t suggest a Region3 API check because that’ll be considering potentially hundreds of parts you simply aren’t interested in as (I assume you only care about HumanoidRootParts).
I’d suggest you try this out and report your findings here so people with the same question in future can refer to this.
4 Likes
There’s an API where you can filter the results to only include humanoidrootparts.
2 Likes
https://twitter.com/PhoenixSignsRBX/status/1023632445982175233
That’s an early prototype and it seems to work just fine. The lag is due to physics calculation from mobs dying.