Store the field data of an entity - AWS SimSpace Weaver

Store the field data of an entity

The following examples demonstrate how you can store (write to the state fabric) the field data of an entity that the app owns. These examples use the following function:

AWS_WEAVERRUNTIME_API Result<void> StoreEntityField( Transaction& txn, const Entity& entity, TypeId keyTypeId, FieldIndex index, std::int8_t* src, std::size_t length) noexcept;

The Api::TypeId keyTypeId parameter represents the data type of the passed in data.

The Api::TypeId keyTypeId parameter should receive the corresponding Api::TypeId from Api::BuiltinTypeId. If there is no appropriate conversion, you can use Api::BuiltinTypeId::Dynamic.

For complex data types, use Api::BuiltInTypeId::Dynamic.

Note

The value of FieldIndex index must be greater than 0. The value 0 is reserved for the index key (see StoreEntityIndexKey()).

Example using primitive data types
namespace { constexpr Api::FieldIndex k_isTrueFieldId { /* value */ 1 }; } Result<void> SetEntityFields( Api::Entity& entity, Transaction& transaction) { bool value = true; auto* src = reinterpret_cast<std::int8_t*>(value); size_t length = sizeof(*value); WEAVERRUNTIME_TRY(Api::StoreEntityField( transaction, entity, Api::BuiltinTypeIdToTypeId( Aws::WeaverRuntime::Api::BuiltinTypeId::Bool), k_isTrueFieldId, src, length)); }
Example using a struct to hold the data
namespace { constexpr Api::FieldIndex k_dataFieldId { /* value */ 1 }; } struct Data { bool boolData; float floatData; }; Result<void> SetEntityFields( Api::Entity& entity, Transaction& transaction) { Data data = { /* boolData */ false, /* floatData */ -25.93 }; auto* src = reinterpret_cast<std::int8_t*>(data); size_t length = sizeof(*data); WEAVERRUNTIME_TRY(Api::StoreEntityField( transaction, entity, Api::BuiltinTypeIdToTypeId( Aws::WeaverRuntime::Api::BuiltinTypeId::Dynamic), k_dataFieldId, src, length)); }