Query for changed entities
Updated: Mar 4, 2026
The changedSince query is an advanced, experimental API. It retrieves entities that changed since a specified DataModel version. The DataModel version increments each time the DataModel changes.
The changedSince query takes two parameters:
- The component ID to track changes for
- A
DataModel version number
When the query runs, it returns only entities whose specified component has been modified since the provided version number.
@OptIn(SpatialSDKExperimentalAPI::class)
class GrabPhysicsSystem : SystemBase() {
private var lastVersion = 0UL
override fun execute() {
val currentVersion = EntityContext.getDataModel()!!.getLastUpdateVersion()
val q = Query.where { changedSince(Grabbable.id, lastVersion) and has(Physics.id) }
for (entity in q.eval()) {
// Grabbable changed since lastVersion — update physics state
}
lastVersion = currentVersion
}
override fun getDependencies(): SystemDependencies? {
return SystemDependencies(
mustRunAfter = mutableSetOf(SystemDependencyConfig(TickPhysicsSystem::class)))
}
}
Use getDependencies() to control execution order relative to other systems. In this example, the system runs after TickPhysicsSystem so it can react to physics-related grab changes.
To use changedSince queries effectively, you need to:
- Maintain a version number variable (like
lastUpdateVersion in the example) - Update this version number after processing the query results
- Use this updated version number in the next query execution
The current version of the data model can be obtained using the getLastUpdateVersion method of the DataModel class:
@SpatialSDKExperimentalAPI
fun getLastUpdateVersion(): ULong {
//
}