Scripting Helpers is winding down operations and is now read-only. More info→
Ad
Log in to vote
0

Best way for mob player detection?

Asked by 7 years ago

Right now my method is to have the individual mobs scan the player list to see if any people are close using magnitudes. Is there a more efficient way to do this?

0
If you have a lot of mobs/players then there might be a noticeable CPU spike when updating them all. You can avoid this by distributing the mob updates over time, i.e. update 10 mobs now, wait a moment, update 10 more, wait a moment, etc... duckwit 1404 — 7y

2 answers

Log in to vote
0
Answered by 7 years ago

The other way could be Raycasting, possibly more efficient and will not lock onto players if they're hidden behind objects.

1
Raycasting is definitely more computationally expensive than a magnitude check. Avoid unnecessary rays whenever possible, especially if a distance check will suffice! duckwit 1404 — 7y
Ad
Log in to vote
0
Answered by
duckwit 1404 Moderation Voter
7 years ago

There are some optimisations you can add if you need better performance, but the rule of thumb is only optimise if you need to. If you haven't encountered a performance bottleneck, then you shouldn't worry about small things like this.

If you have lots of mobs updating their targeting simultaneously, you'll want to avoid performing the same computation over and over again for each of them. Instead, you'll want to cache some data about where players are which will be useful for all of the mobs and make it easier for them, individually, to make a decision.

One way to do this is to divide space into a grid of cells (either 3D or 2D, depending on your game). Each cell should be about as big as the detection range (assuming this is the same for every mob). Every time you update which mobs are targeting which players, fill the grid in such that each entry for a cell is a list of which players are in it.

Each mob then only checks whether it should target players either in the cell that it is in or an adjacent cell - if a player is far away from a mob, they won't be checked by the mob because they won't be in an adjacent cell. The mob will only query the distance (or raycast for line-of-sight) to players in nearby cells. Which players are in which cells will have already been computed at this point.

If you don't have lots of mobs, though, (at least a few hundred!) then this won't make much difference.

Any such optimisations really depend on the nature of your circumstances. Just how many mobs are we talking about, 2D or 3D, how often do they update, their assumptions about the world, etc...

For a similar, even more sophisticated technique, check out quadtrees and octrees.

0
Do you think I could use the reverse method of what my current system is, a local script in the player would scan through the mobs to see if any of them were close enough to attack. Then fire some event or something to chase. Would probably put less stress on the server. QuantumToast 261 — 7y
0
cause rn I cant really use cells cause the detection range differs QuantumToast 261 — 7y
0
You can still use a cell technique if you set the cell size according to the maximum range of a mob. Yes, you could distribute calculations to the clients and have each player communicate to the server which mobs are nearby. This method includes more network traffic though, which you might also want to minimise. That's partly why you shouldn't optimise unless there is a bottleneck, because ... duckwit 1404 — 7y
0
... because you don't know what to optimise for (network usage, server load, client load, latency, etc...). duckwit 1404 — 7y
View all comments (4 more)
0
One of the issues is that the range of each mob is effected by player stats and stuff. QuantumToast 261 — 7y
0
Choose the least upper bound (LUB) on mob range and use that. That is, find the smallest range that is still bigger than all of them. How effective this technique is also depends on the size of your map. duckwit 1404 — 7y
0
If you have equations that relate player stats to mob range, just maximise them; find the highest value it could take (assuming it isn't infinite). A similar technique would be to create special zones where your mobs check for players, but don't look outside of them. This is like the cell technique but more fluid. duckwit 1404 — 7y
0
Without further details about the environment / world assumptions and what the performance problem is, there isn't anything left to optimise here. Engineering. If it ain't broke, don't fix it! :) duckwit 1404 — 7y

Answer this question