0

I was trying to create a contract that has multiple rooms, in each room there would be some amount of players. Pls how do I acheive this? This is my approach but it's not working. I am unable to return the mapped array also.

contract TheGame {
    uint256 totalRoom;

    struct Room {
        address player;
        uint256 amount;
    }
    Room[] public room;

    mapping(uint256 => Room[]) public eachRoomNumberToEachRoom;

    function createRoom(uint256 _amount) public {
        room.push(Room({
            player: msg.sender,
            amount: _amount
        }));
        totalRoom += 1;
        eachRoomNumberToEachRoom[totalRoom] = room;
    }

    function getEachRoom(uint256 _index) public view returns () {
        return eachRoomNumberToEachRoom[totalRoom][_index];
    }
}

2 Answers 2

0

Follow NESTED_ARRAYS_NOT_IMPLEMENTED, you will valuable information.

1
  • I don't get most of the thing clearly tough, thanks you for sharing
    – Paulliano
    Commented Jul 26, 2022 at 12:19
0

You need to specify the type of the return statement when expecting the function that returns a value, so your getEachRoom function needs to specify that it's returning a Room object that is stored in the memory. So the getEachRoom should look like this:

    function getEachRoom(uint256 _index) public view returns (Room memory) {
        return eachRoomNumberToEachRoom[totalRoom][_index];
    }

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.