Memuat data bidang entitas yang dihapus - AWS SimSpace Weaver

Terjemahan disediakan oleh mesin penerjemah. Jika konten terjemahan yang diberikan bertentangan dengan versi bahasa Inggris aslinya, utamakan versi bahasa Inggris.

Memuat data bidang entitas yang dihapus

Anda tidak dapat memuat (membaca dari struktur status) data bidang entitas untuk entitas yang telah dihapus dari area kepemilikan dan langganan aplikasi. Contoh berikut menghasilkan kesalahan karena memanggil Api::LoadIndexKey() entitas sebagai akibat dariApi::ChangeListAction::Remove. Contoh kedua menunjukkan cara yang benar untuk menyimpan dan memuat data entitas secara langsung di aplikasi.

contoh Contoh kode yang salah
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; } } } /* ... */ }
contoh Contoh cara yang benar untuk menyimpan dan memuat data entitas di aplikasi
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; } } } /* ... */ }