struct Zombie {
string name;
uint dna;
uint32 level;
uint32 readyTime;
uint16 winCount;
uint16 lossCount;
}
Zombie[] public zombies;
function _createZombie(string memory _name, uint _dna) internal {
uint id = zombies.push(Zombie(_name, _dna, 1, uint32(now + cooldownTime), 0, 0)) - 1;
}
Please explain how the above line code uint id generates an id for zombies in the _createZombie function.
When zombies.push()
happens, it returns the new length of array zombies
so, it gets saved in the id
as zombies.length - 1
which gives you current index of the Zombie data stored.
Note: I guess this code snippet will not run on latest versions of solidity compiler, as there would be some warnings.