I am writing a smart contract to be the backend of a game. Instead of using a mapping, I have decided to use an array to store the addresses of accounts who have paid my contract. (I know it is common practice to use a mapping for this but I find them to be confusing)
address[] paidPlayers;
As players join, their address is added via the following function.
function addPlayer(address player) constant returns (address[]) {
paidPlayers.push(player);
return paidPlayers;
}
Periodically, when a round in the game is completed, I want my contract to reset. I thus need a way of emptying my paidPlayers array. How is this done in solidity? Is there anything like the javascript
paidPlayers = [];
I have found methods for deleting a specific element but not the array as a whole.
An alternative is to use mappings of uint256 and a totalNumberOfElements to handle where to save the values. In case of clear/reset you set totalNumberOfElements = 0.
For inserts, you should use myArray[totalNumberOfElements] = value
– imazzara Jan 03 '19 at 15:06The rule should be: "If you are not confident about the size of the array, you will need to use mappings to prevent a possible DoS."
Moreover, it saves gas, cause you are re-using the slots instead of empty all of them.
– imazzara Jan 03 '19 at 16:02length_of_the_array? Please see (https://ethereum.stackexchange.com/q/69882/4575) for more detail in question. @Ismael – alper Apr 19 '19 at 19:02deletehas not been supported since 0.6 version. There is no info about it in their press release https://blog.soliditylang.org/2019/12/17/solidity-0.6.0-release-announcement/, but it is mentioned in https://docs.soliditylang.org/en/v0.8.11/060-breaking-changes.html?highlight=Pop#explicitness-requirements – Gleichmut Aug 11 '22 at 13:08