val dataModel = EntityContext.getDataModel()
// setup button event listener
button.entity.registerEventListener<EventArgs>("button") { _, _ ->
// handle event
}
dataModel?.sendEvent(entity, "button", EventArgs("click", dataModel))
class DataModel(dataModel_: Long)
DataModel
(
dataModel_
)
|
bumpComponentVersion
(
entityId
, componentId
)
|
Bumps the component version, marking it as "changed" without modifying data.
This forces a component to be considered changed for change detection purposes, even when the actual component data hasn't been modified. This will trigger component change listeners and queries that look for changed components.
Signature
fun bumpComponentVersion(entityId: Long, componentId: Int) Parameters entityId: Long
The ID of the entity.
componentId: Int
The ID of the component to mark as changed.
|
createEntity
()
|
Creates a new entity in the data model.
Signature
fun createEntity(): Entity |
createEntity
(
components
)
|
Creates a new entity in the data model with components.
Signature
fun createEntity(components: List<ComponentBase>): Entity Parameters components: List |
createEntity
(
components
)
|
Creates a new entity in the data model with components.
Signature
fun createEntity(vararg components: ComponentBase): Entity Parameters components: ComponentBase |
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
)
|
Gets all listeners registered for a specific event type.
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
)
|
Signature
inline fun <T, V> getKeyValueMap(id: Long, attribute: Int): HashMap<T, V> Parameters id: Longattribute: IntReturns HashMap |
getLastUpdateVersion
()
|
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
)
|
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
)
|
Signature
external fun nativeGetKeyValueMap(dataModel: Long, id: Long, attribute: Int): Array<Any> Parameters dataModel: Longid: Longattribute: IntReturns 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.
|
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.
|
registerEventListener
(
entity
, eventType
, listener
)
|
Registers a listener for events on a specific entity.
Signature
fun registerEventListener(entity: Entity, eventType: String, listener: EventListener<EventArgs>) Parameters eventType: String
The type of event to listen for.
|
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.
|
getLocalDataModelTime
()
|
Gets the local time based on the data model.
Signature
fun getLocalDataModelTime(): Long Returns Long |
inner class KeyIterator(dm_: DataModel, it_: Long)
KeyIterator
(
dm_
, it_
)
|
Signature
constructor(dm_: DataModel, it_: Long) Parameters dm_: DataModelit_: LongReturns DataModel.KeyIterator |
close
()
|
Signature
fun close() |
end
()
|
Signature
fun end(): Boolean Returns Boolean |
entity
()
|
Signature
fun entity(): Entity Returns Entity |
finalize
()
|
Signature
fun finalize() |
next
()
|
Signature
operator fun next() |