I am working on my custom server for my online game and I was wondering if there is any way to accomplish this in a more performant way.
Basically I will be spawning and despawning objects (such as other players) to clients based on the distance between their positions. Basically, if you are within, say, 500 meters away from another character, the server will send you a "Spawn character" packet to spawn that character on your client. If you move more than 500 meters away from that client, the server will send you a despawn packet.
The only way I can think of doing this on the server is a nested for loop. For every character on the server, check every other character position. That's expensive and obviously as the character count grows, so does the time it takes for process the loops.
Can anyone think of any different approach to this that will be better performant? Most survival games don't do this, and you just have all characters spawned on every client, but this allows for ESP hacks (Extra Sensory Perception which basically is a hack that shows you exactly where everyone is, even if the client isn't rendering them because they are far away) and I don't want that as a possibility.
If you need any other information let me know, thanks.