API reference
API reference
Select your platform
No SDKs available
No versions available

DataModel Class

Modifiers: final
DataModel is a class that manages data for Entities, Components, and Attributes in the Spatial SDK ECS architecture.
For debugging and introspecting into the live state of your datamodel, register the DataModelInspectorFeature.
The DataModel is also responsible for passing custom events to listeners. Example:
val dataModel = EntityContext.getDataModel()
// setup button event listener
button.entity.registerEventListener<EventArgs>("button") { _, _ ->
  // handle event
}
dataModel?.sendEvent(entity, "button", EventArgs("click", dataModel))

Also see PhysicsSample sample project for a more in depth example of registering custom event listeners.

Signature

class DataModel(dataModel_: Long)

Constructors

DataModel ( dataModel_ ) : DataModel
Signature
constructor(dataModel_: Long)
Parameters
dataModel_: Long
Returns

Functions

createEntity () : Entity
Creates a new entity in the data model.
Signature
fun createEntity(): Entity
Returns
The Entity.
createEntity ( components ) : Entity
Creates a new entity in the data model with components.
Signature
fun createEntity(components: List<ComponentBase>): Entity
Parameters
components: List
Returns
The Entity.
createEntity ( components ) : Entity
Creates a new entity in the data model with components.
Signature
fun createEntity(vararg components: ComponentBase): Entity
Parameters
components: ComponentBase
Returns
The Entity.
deleteEntity ( eid )
Deletes an entity from the data model. The entity will be included in deleted queries next tick. We recommend using Entity.destroy() instead of this function.
Signature
fun deleteEntity(eid: Long)
Parameters
eid: Long
The ID of the entity to delete.
getEventListenerForEvent ( eventType ) : Set
Gets all listeners registered for a specific event type.
Signature
fun getEventListenerForEvent(eventType: String): Set<<Error class: unknown class><Entity, EventListener<EventArgs>>>
Parameters
eventType: String
The type of event to get listeners for.
Returns
Set
A set of entity-listener pairs for the specified event type.
getKeyValueMap ( id , attribute ) : HashMap
Signature
inline fun <T, V> getKeyValueMap(id: Long, attribute: Int): HashMap<T, V>
Parameters
id: Long
attribute: Int
Returns
HashMap
getLastUpdateVersion () : ULong
Experimental API. This gets a long that represents the last updated version of the data model.
This is important for making updates the same tick as other component changes. Example:
class MyComponentSystem() : SystemBase() {
 private var lastUpdateVersion = 0UL
 @OptIn(SpatialSDKExperimentalAPI::class)
 override fun execute() {
   for (entity in Query.where { changedSince(MyComponent.id, lastUpdateVersion) }.eval()) {
     // handle changing entity
   }
   lastUpdateVersion = EntityContext.getDataModel()!!.getLastUpdateVersion()
 }
}

Signature
fun getLastUpdateVersion(): ULong
Returns
ULong
hasKey ( id , attribute ) : Boolean
Checks if an entity has a specific attribute.
Signature
fun hasKey(id: Long, attribute: Int): Boolean
Parameters
id: Long
The ID of the entity.
attribute: Int
The attribute ID to check for.
Returns
Boolean
True if the entity has the attribute, false otherwise.
nativeGetKeyValueMap ( dataModel , id , attribute ) : Array
Signature
external fun nativeGetKeyValueMap(dataModel: Long, id: Long, attribute: Int): Array<Any>
Parameters
dataModel: Long
id: Long
attribute: Int
Returns
Array
registerAttributeListener ( attribute , listener )
Registers a listener for changes to a specific attribute.
Signature
fun registerAttributeListener(attribute: Int, listener: AttributeListener)
Parameters
attribute: Int
The ID of the attribute to listen for changes on.
The callback to invoke when the attribute changes.
registerComponentListener ( componentID , componentClass , listener )
Registers a listener for changes to a specific component.
Signature
fun registerComponentListener(componentID: Int, componentClass: KClass<*>, listener: ComponentListener)
Parameters
componentID: Int
The ID of the component to listen for changes on.
componentClass: KClass
The class of the component.
The callback to invoke when the component changes.
registerEventListener ( entity , eventType , listener )
Registers a listener for events on a specific entity.
Signature
fun registerEventListener(entity: Entity, eventType: String, listener: EventListener<EventArgs>)
Parameters
entity: Entity
The entity to listen for events on.
eventType: String
The type of event to listen for.
listener: EventListener
The callback to invoke when the event occurs.
registerLinkedEntityAttribute ( attributeID )
This method is experimental and is subject to change in the future.
Registering a linked entity attribute makes it so that the DataModel will build a hierarchy based on the given entity attribute. This means that you can query for children of a given entity based on the registered linked entity attribute.
Example:
In Your Activity's OnCreate:
EntityContext.getDataModel()?.registerLinkedEntityAttribute(MyComponent.myEntityAttributeData)

Then you can query for children of a certain entity (Where their MyComponent.myEntityAttribute is myEntity) in a System Query:
val query = Query.where { childrenOf(myEntity, MyComponent.myEntityAttributeData) }

Signature
fun registerLinkedEntityAttribute(attributeID: Int)
Parameters
attributeID: Int
removeComponentListener ( componentID )
Removes a previously registered component listener.
Signature
fun removeComponentListener(componentID: Int)
Parameters
componentID: Int
The ID of the component to stop listening for changes on.

Companion Object

Companion Object Functions

getLocalDataModelTime () : Long
Gets the local time based on the data model.
Signature
fun getLocalDataModelTime(): Long
Returns
Long

Inner Class

KeyIterator Class

Modifiers: final

Signature

inner class KeyIterator(dm_: DataModel, it_: Long)

Constructors

KeyIterator ( dm_ , it_ ) : DataModel.KeyIterator
Signature
constructor(dm_: DataModel, it_: Long)
Parameters
dm_: DataModel
it_: Long
Returns
DataModel.KeyIterator

Functions

close ()
Signature
fun close()
end () : Boolean
Signature
fun end(): Boolean
Returns
Boolean
entity () : Entity
Signature
fun entity(): Entity
Returns
finalize ()
Signature
fun finalize()
next ()
Signature
operator fun next()
Did you find this page helpful?
Thumbs up icon
Thumbs down icon