Table of contents
In week 3 of the Alchemys’ Roadtoweb3 tutorial, we created a Dynamic NFTs with On-Chain Metadata🚀🚀🚀, GitHub repo Here.
In this week's lesson, there was a lot to learn. But what we are focusing on in this article are Mapping, Structs, and arrays in Solidity.
Time to make it a little less complicated and not confusing😉
What are structs?
In solidity, structs enable you to create complex data types with multiple variables. You can create new custom-defined types and group several variables in Solidity using structs. Think of them as containers for your data in Solidity. They let you bundle together different variables into a neat package.
Let's do a little exercise to help us understand this better😎
Imagine you're creating a game where each character has unique traits. With structs, you can define these traits concisely, like this:
struct Traits {
string name;
uint level;
uint speed
bool strength;
uint lives;
}
In this snippet, we defined the struct
and gave it the name "Traits". Inside the struct
, we list variables like name, level, speed, strength, and lives. These variables can hold different types of data, like strings, integers, and booleans.
Mapping
In solidity, mapping is used for storing data, and they are only allowed for state variables. Mapping has a KeyType
and a ValueType
for storing data.
mapping(KeyType => ValueType)
KeyType
can be simple built-in data types such as bytes, uint, string, etc.ValueType
can be either simple or complex data types such as array and struct.
Think of mappings as virtual hash tables initialized with all keys mapped to zero values. Essentially, you've got a blank slate for each item you want to store.
Mappings can be seen as hash tables which are virtually initialized such that every possible key exists and is mapped to a value whose byte-representation is all zeros: a type’s default value
Continuing from our exercise above
mapping(uint256 => Traits) public tokenIdToTraits;
function getName() public view returns (string memory) {
Traits memory _trait = tokenIdToTraits[tokenId];
return _trait.name();
}
mapping(uint256 => Traits) public tokenIdToTraits
- defines a mapping namedtokenIdToTraits
that usesuint256
as the key type andTraits
as the value type.- The
getName
function allows us to retrieve the name of a game character based on their tokenId.
Array
An array is a data structure that can have a fixed size or a dynamic size. An array is used to store simple data types, it can also be used to store a struct.
From the exercise we’ve started, let’s store our array in a struct.
First, we create an array of characters to add to our game, create new characters, and push the new characters we created to the array.
//create an array
Traits[] public newTraits;
//create a new game character
Traits newCharacter = Traits(“Warrior”, 3, 5, true, 4);
//Push new game characters to our array
newTraits.push(newCharacter);
To call any struct member, we use the access operator(.), the access operator(.) is placed between the struct variable name and the struct member that we want to call. From our exercise above let's call the struct member - speed
function getSpeed() public view returns (uint) {
return newTraits.speed;
}
Conclusion
Finally, If you have come this far you have learned a lot about structs, mappings, and arrays, You deserve applause, so take a break, relax you deserve it 🚀👏