When I left off the last time, I had just finished creating the basic functionality for consuming potions to gain health. Now I’m moving on to modifying mutable attributes on tokens on the WAX Blockchain.
What are Mutable Attributes?
NFTs on the WAX Blockchain have two different types of data associated with them: Immutable Data, and Mutable Data. Immutable data is set when the NFT is created, and can NOT be changed after the NFT is minted, making these data points a permeant and unchangeable data property. This is useful for things like the token name, image, etc.
![viewing immutable and mutable data](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-1.37.47-PM-1024x481.png)
Mutable Data on the other hand, is set after the NFT is minted, and can be modified through API calls to the blockchain. The owner of the collection can execute these calls to set mutable data regardless of what wallet they reside in, and without the authority of the user that holds the NFT. I’ll be using this mutable data to keep track of the player character’s Level and Experience Points.
Modifying mutable attributes: Make it Work!
I’m going to start on the backend, working on the API route to accept parameters for the Character’s NFT ID and the Enemy’s NFT ID.
![getting ready to being modifying mutable attributes](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-1.52.28-PM-1024x589.png)
After getting the IDs, I’ll search for the Character in the Database, then search the blockchain for the Enemy. Each Enemy NFT has an immutable data field for XP with an integer value, and once I have my enemy and character objects, I’ll pull the XP given by the enemy from the object and add it to the existing Experience Points the character has gained.
const experienceGained = enemy.data.data.immutable_data.xp + character.experiencePoints;
Create the Action
Now I have to build the action for the transaction. As I said in previous updates, this is going to look pretty familiar, but now we’re sending in a new data point: new_mutable_data
![creating the action to begin modifying mutable attributes](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-2.03.56-PM-1024x633.png)
The new_mutable_data expects the data as you can see in the action. The property will be defined by specifying the “key” and then “value” and in the case of our action will be modifying the character’s “level” and “experiencePoints” properties. Mutable data can and will be overwritten each time this API is called, and you want to make sure that all data points are set to avoid null value problems. I’m only modifying the experiencePoints value, but I’m passing the current Character’s Level into the Level value so that it remains after the update.
All that is left to do is to push the transaction, modifying mutable attributes! I’ve shown this a few times now, but here it is again:
![sending the transaction for modifying mutable attributes](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-2.18.17-PM-1024x390.png)
Now I can go to the NFT to see what I’ve done! I prefer viewing this kind of information on AtomicHub.io but you can view it most places.
![viewing the modified mutable attributes](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-2.23.12-PM-1024x175.png)
As you can see from the blockchain, a full history of the character’s activities will be saved. Each time this function runs, a new line like you see above will be added to the record. Old Data an New Data are visible, but only the new data will be set to the token. Here’s how it looks again when simply viewing the NFT:
![viewing NFT immutable and mutable data](https://www.ryanemmons.dev/wp-content/uploads/2023/09/Screenshot-2023-09-23-at-1.37.47-PM-1024x481.png)
Wrapping Up
Modifying mutable attributes is another big milestone update, but there’s still work to be done here. I need to add logic for leveling up the character and add this call to the battle rewards logic to give the characters experience after the enemy is defeated. I’m going to be spending my time doing that and testing and squashing bugs along the way. Most of my required functionality is now built, so things might get a little quiet for game updates for a few weeks, but I’ll be sure to post about anything interesting along the way!
Leave a Reply