Storage variables can be marked as public
during declaration:
publicName: public(String[64])
The compiler automatically creates getter functions for all public
storage variables. These getter functions are NOT written in your code, but are generated by the compiler when it compiles it.
For the example above, the compiler will generate a function named publicName
that does not take any arguments and returns an String[64]
, the value of the state variable publicName
. So, even if you cannot see any function named publicName
, it will be generated when your code is compiled.
We need a public storage variable that tracks the number of pokemons created in the contract.
-
Create a
public
storage variable namedtotalPokemonCount
of typeuint256
. -
Replace the key
0
in the mappingpokemonList
in the function_createPokemon
with the storage variabletotalPokemonCount
. Remember to use theself
environment variable to access the storage variable from the function. -
In the
_createPokemon
function, incrementtotalPokemonCount
by 1 using theself
environment variable. To make the code look clean use the+=
arithmetic operator as shown below:# adds 1 to the parameter passed @external def addOne(number: uint256): # we used += to increment number by 1 number += 1