Improbable Icon

Calculating Total Entities of a Certain Type

how-to

#1

What is the best way to access the total number of entities of a certain type that are active in the world?

A good example would be to say how many players are online. Also, having this number update frequently to reflect the state of the world more accurately would be desired, so performance is definitely a concern.


#2

I believe this might be what you’re looking for!
Querying World


#3

i remember reading that that was slow. ill look more into it. thanks


#4

Hi there @AnTennA!

For your specific requirements (accurate value, frequent updates, frequent querying) the best thing I can think of is to have a dedicated world-state entity with separate components for each type of value (player-count, …) that you want to keep track of. Whenever an arbitrary worker performs an action that modifies one of the world-values it should fire off a command to the relevant world-state component telling about this update.

Because commands are updated by the same worker (i.e the one authoritative over the world-state component) they are processed sequentially and, as a result, you should not have much issues of atomically updating the variables, aside from verifying that your command got processed correctly and retry if not.

The point of having a separate component for each global value instead of putting all values into a single WorldState component is so that when you start scaling you will be able to split up the handling of updates over different workers instead of a single one that would get swamped by update-commands. If you start hitting performance issues even with this approach then you have scaled up quite a lot and it will be worth investigating multiple global-state entities combined with consensus algorithms or other alternatives.

Best regards,
Duco


#5

awesome. thanks, Duco!