Skip to content

Entity System Design Q A

jacobdufault edited this page Jan 21, 2014 · 1 revision

Why isn't there a IEntity.TryModify?

Let's see why a TryModify function would be useful. The user case is simple: you have a data type which does not support concurrent modifications. In system code, someone else may have modified the data so you want to make sure that it's available for modification before modifying it.

Here's the problem with this, and it has to deal with concurrency. For simplicity, lets say that there are two systems, System A and System B. Both systems want to modify the data using TryModify. On computer A, System A executes first and TryModify returns true. On computer B, System B executes first and it modifies the data.

Do you see the issue? TryModify makes it extremely easy to introduce synchronization bugs. In the scenario described previously, computer A and computer B have separate simulations.

If two systems need to modify the same data type in the same update, the data type needs to extend IConcurrent and all operations on that data type need to be commutative w.r.t. other operations (so for a data type with only an int, you can only add/subtract from it, not multiply).

Clone this wiki locally