本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。
載入已移除實體的欄位資料
您無法為已從應用程式擁有權和訂閱區域移除的實體載入 (從 state Fabric 讀取) 實體欄位資料。下列範例會導致錯誤,因為它會呼叫Api::LoadIndexKey()
實體的結果Api::ChangeListAction::Remove
。第二個示例顯示了直接在應用程序中存儲和加載實體數據的正確方法。
範例 不正確的程式碼範例
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; } } } /* ... */ }
範例 在應用程序中存儲和加載實體數據的正確方法示例
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; } } } /* ... */ }