Loading the field data of removed entities - AWS SimSpace Weaver

Loading the field data of removed entities

You can’t load (read from the state fabric) entity field data for entities that have been removed from the app’s ownership and subscription areas. The following example results in an error because it calls Api::LoadIndexKey() on an entity as a result of an Api::ChangeListAction::Remove. The second example shows a correct way to store and load entity data directly in the app.

Example of incorrect code
Result<void> ProcessSubscriptionChanges(Transaction& transaction) { /* ... */ WEAVERRUNTIME_TRY(Api::SubscriptionChangeList subscriptionChangeList, Api::AllSubscriptionEvents(transaction)); for (const Api::SubscriptionEvent& event : subscriptionChangeList.changes) { switch (event.action) { case Api::ChangeListAction::Remove: { std::int8_t* dest = nullptr; /** * Error! * This calls LoadEntityIndexKey on an entity that * has been removed from the subscription area. */ WEAVERRUNTIME_TRY(Api::LoadEntityIndexKey( transaction, event.entity, Api::BuiltinTypeIdToTypeId( Api::BuiltinTypeId::Vector3F32), &dest)); AZ::Vector3 position = *reinterpret_cast<AZ::Vector3*>(dest); break; } } } /* ... */ }
Example of a correct way to store and load entity data in the app
Result<void> ReadAndSaveSubscribedEntityPositions(Transaction& transaction) { static std::unordered_map<Api::EntityId, AZ::Vector3> positionsBySubscribedEntity; WEAVERRUNTIME_TRY(Api::SubscriptionChangeList subscriptionChangeList, Api::AllSubscriptionEvents(transaction)); for (const Api::SubscriptionEvent& event : subscriptionChangeList.changes) { switch (event.action) { case Api::ChangeListAction::Add: { std::int8_t* dest = nullptr; /** * Add the position when the entity is added. */ WEAVERRUNTIME_TRY(Api::LoadEntityIndexKey( transaction, event.entity, Api::BuiltinTypeIdToTypeId( Api::BuiltinTypeId::Vector3F32), &dest)); AZ::Vector3 position = *reinterpret_cast<AZ::Vector3*>(dest); positionsBySubscribedEntity.emplace( event.entity.descriptor->id, position); break; } case Api::ChangeListAction::Update: { std::int8_t* dest = nullptr; /** * Update the position when the entity is updated. */ WEAVERRUNTIME_TRY(Api::LoadEntityIndexKey( transaction, event.entity, Api::BuiltinTypeIdToTypeId( Api::BuiltinTypeId::Vector3F32), &dest)); AZ::Vector3 position = *reinterpret_cast<AZ::Vector3*>(dest); positionsBySubscribedEntity[event.entity.descriptor->id] = position; break; } case Api::ChangeListAction::Remove: { /** * Load the position when the entity is removed. */ AZ::Vector3 position = positionsBySubscribedEntity[ event.entity.descriptor->id]; /** * Do something with position... */ break; } } } /* ... */ }