Solidity Arrays – Help Using and Reading Arrays and Mappings in Solidity

go-ethereummappingsolidity

I'm trying to do something very simple. I want to make a structure that holds the data for a player.

Within that data, I want an array to store things like its inventory, and its current eggs/monsters.

Yet I cannot interact with arrays at all, I'm constantly getting issues. Can someone tell me what I'm doing wrong and why it won't let me read and return arrays?

contract EtherMon{ 
    uint public starterEgg;
    address public owner;



    struct player{
        uint256 goldAmount;
      uint[10] listOfEggs;
        bool eggCoolDown;
        uint eggTime;
        uint eggID;
        bool battleCoolDown;
        bool breedingCoolDown;
        uint[] ownedEthermon;
    }

      mapping (address => player) public listOfPlayers;

    function EtherMon(){
        owner = msg.sender;

    }

    function createAccount(){


     starterEgg = block.timestamp % 10;

     listOfPlayers[msg.sender].goldAmount  = 100;
     listOfPlayers[msg.sender].listOfEggs[0] = starterEgg;


        }



  function getCurrentEggs() returns (uint[]) {
 uint length =  listOfPlayers[msg.sender].listOfEggs.length;
 uint[] eggArray;
            for(uint i = 0; i < length; i++){
               eggArray[i] =  listOfPlayers[msg.sender].listOfEggs[i];
            }
            return eggArray;
        }

    }

Basically I just want to be able to…

  1. Call CreateAccount and it stores a random uint into the array that stores the eggs a player holds
  2. Pull that egg data from the mapping and see it/change it

Can someone give me an example of a structure that holds an array (and mapping because it seems I can use a mapping within a struct but have issues with dynamic arrays) and has getter and setter functions?

Best Answer

Here are some patterns that might help you out.

pragma solidity ^0.4.15;

contract Storage {

    struct PlayerStruct {
        uint meaningless;
        uint[] dynamicList;
        uint[10] fixedList;
        mapping(bytes32 => uint) keyToUintMap;
    }

    mapping(address => PlayerStruct) public playerStructs;

    function getPlayerDynListLength(address player) public constant returns(uint count) {
        return playerStructs[player].dynamicList.length;
    }

    function appendPlayerDynList(address player, uint value) public returns(uint length) {
        return playerStructs[player].dynamicList.push(value);
    }

    function setPlayerDynFixedList(address player, uint index, uint value) public returns(bool success) {
        require(index <= 9);
        playerStructs[player].fixedList[index] = value;
        return true;
    }

    function getPlayerDynamicListElement(address player, uint index) public constant returns(uint value) {
        return playerStructs[player].dynamicList[index];
    }

    function setPlayerMappedElement(address player, bytes32 key, uint value) public returns(bool success) {
        playerStructs[player].keyToUintMap[key] = value;
        return true;
    }

    function getPlayerMappedElement(address player, bytes32 key) public constant returns(uint value) {
        return playerStructs[player].keyToUintMap[key];
    }

}

Update:

Here it is in Remix to show it working:

enter image description here

Hope it helps.

Related Topic