Sui.

Post

Share your knowledge.

lite.vue.
Aug 26, 2025
Expert Q&A

How Do I Use Dynamic Fields in Sui?

How Do I Use Dynamic Fields in Sui?

  • Sui
  • SDKs and Developer Tools
  • Transaction Processing
0
6
Share
Comments
.

Answers

6
dhaholar.
Aug 26 2025, 22:30

You 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)
}
0
Comments
.
Opiiii.
Opiiii1029
Aug 26 2025, 22:57

(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

0
Comments
.

Do you know the answer?

Please log in and share it.