Post
Share your knowledge.
How Do I Use Dynamic Fields in Sui?
How Do I Use Dynamic Fields in Sui?
- Sui
- SDKs and Developer Tools
- Transaction Processing
Answers
6You use dynamic fields in Sui to attach flexible, runtime-defined data to objects without needing to declare every field in advance. These fields let you store values with custom names—like strings or numbers—and access or update them as needed. You can add a dynamic field using the add function from the sui::dynamic_field module, and retrieve it later with borrow or borrow_mut. This is especially useful for building collections or inventories where the structure changes over time. To remove a field, you use remove or remove_if_exists, which also helps manage storage costs.
Here’s a basic example:
public fun add_field(parent: &mut Parent, name: vector<u8>, value: u64) {
sui::dynamic_field::add(&mut parent.id, name, value);
}
public fun get_field(parent: &Parent, name: vector<u8>): &u64 {
sui::dynamic_field::borrow<u8, u64>(&parent.id, name)
}
(sui::dynamic_field): Stores any value with the store ability. However, if it's an object, it's wrapped and not directly accessible via its ID externally. Dynamic Object Field (sui::dynamic_object_field): Stores key, store objects and retains exter
Do you know the answer?
Please log in and share it.
Sui is a Layer 1 protocol blockchain designed as the first internet-scale programmable blockchain platform.
- How to Maximize Profit Holding SUI: Sui Staking vs Liquid Staking616
- Why does BCS require exact field order for deserialization when Move structs have named fields?65
- Multiple Source Verification Errors" in Sui Move Module Publications - Automated Error Resolution55
- Sui Move Error - Unable to process transaction No valid gas coins found for the transaction419
- Sui Transaction Failing: Objects Reserved for Another Transaction410