I plan to have about 200 AI’s throughout my map. And I need to detect if x player is in range and then trigger the AI to do something.
I’m wondering, would it be more efficient. To just use a region3 with some radius around the player. Or would it be better to constantly run a magnitude check comparing all 200 NPC’s to each player?
I’m thinking that region3 would be better. But I wanted to get some input.
I’m saying that with any use-case involving parts, it’s better to use GetPartBoundsInBox over Region3. I’m not referring to anything about this particular situation other than that. Also I don’t believe that it’s as slow as you make it out to be.
I wrote a boid program, which requires each boid to check the distance of every boid, and with 201 boids, each doing 200 distance checks for a grand total of 40,200 distance calculations 45 times a second, plus a bunch of extra vector math on top of that, it still ran at around 60 fps. In OP’s case, they only need to perform 200 * number of players distance calculations, so unless they have hundreds of players in a single server, they’ll be fine. If they really need to optimize it, they can probably get away with just checking the player’s distance less.
That takes 1.6 milliseconds to do. That’s okay if it’s your entire game, but you most likely have game logic running on top of that, alongside AI checks. I don’t think you want 1/10 of your game’s frame budget being just those distance checks. That’s okay if it’s your entire game I guess, but there’s no reason to be using a lot of your frame budget for nothing. It’s really not that hard to implement spatial hashing.
Pretty much any networking and maybe a dozen humanoids will guarantee your frame times being >12 ms. Besides- it doesn’t take that much effort to do chunking. Just play it safe and don’t abuse your frame budget horrifically. Vector math isn’t your concern- your concern is keeping your frame time low enough your players don’t complain about horrific performance issues with 10 players in-game.
By saying this is going to need optimization, I assume there is a game attached to the AIs. And just to make it clear, I don’t imagine that the server can’t plow through this, but your goal is to keep it as low as possible so you can have a game attached to your AIs. Frame time budget is expensive- and if Roblox ever raises the server fps to 144, it will get even more expensive.
It’s 1 millisecond. And it’s not 8 stud distance.
These functions have many uses, including this situation, however, I’d rather suggest using GetPartBoundsInRadius.
Friendly reminder that 1 millisecond is 1/16th of your frame budget, and if you go over that frame budget you will end up with crippling losses such as: increased ping, overall slowness / visual stuttering, physics stuttering, character lag, etc. etc.
Also reminder that the internal Roblox processes eats up like a minimum of 4-5 milliseconds of that typically. “it’s just 1 millisceond” is just no. Also, I had some tests that took up 13 milliseconds- just don’t use it per frame please…
The OP is clearly talking about running these checks on the server.
I’m not saying “it’s just 1 millisecond”, I just corrected you.
Well, is 200 magnitude checks more performant than a spatial query? I’m not 100% certain but I don’t think so, let me know if I’m wrong.
Oh, and no one said you should use it per frame. You hardly ever need any check to run each frame.
Wait, are you comparing 200 spatial queries to 200 magnitude checks?
The whole point of the spatial check is that you run it only once and it returns all the parts.
And the test should be run with actual parts so you get reliable and realistic results.
If you’re trying to get the closest 200 parts, you should probably design your code so you don’t need the closest 200 parts. That’s a bad idea in the first place.
The way I would do this was in a localscript create a invisible part than has a shape of a ball that is Massless and has CanCollideCanQuery set to false and weld it to the characters rootpart
then I would assign a touch event to the part and when a npcs rootpart touches the invisible part send a remoteevent to the server