2

The main purpose of this code is to add an array of users in mapping. there are certain users who play a video game and are on a certain level now level will be considered as a mapping index and on these level, there will be an array of users. Now the main purpose of taking an array of users is because there can be multiple users on the same level. I just started learning solidity so there might be some logical mistakes in the code can anyone help me and explain to me with this code?

pragma solidity ^0.4.24; 

 contract game
 {

     struct users
     {
         string name;
         uint level;
         uint score;
     }

     mapping(uint => users[]) mulusers;

     function addusers (string _name, uint _level, uint _score) public {

         users memory use= users(_name,_level,_score); 
     }

     function getusers (uint _level) public view returns (string)
     {
        uint index= mulusers[_level].length;
         for(uint i=0; i<index; i++)
         {       
           return mulusers[_level].users[i].name;    //remix is showing an error   
         }   
     }   
 }
1

So i changed your code to what you need i think. Also use PascalCase for Struct and event names and camelCase for function, modifier and variable names. Oh and if your Struct defines only single object (one player, one item, one book etc.) don't use plural name in it :)
P.S. stringToBytes32 function is not mine. I found it somewhere and use it in my code for some time, not sure if it's the most efficient one for converting.

pragma solidity ^0.4.24;

contract game {

    struct User
    {
        string name;
        // If your array to level, 
        // i don't think you really need 
        // it to be stored here since
        // you will already know the level
        // when you call mapping, so save some gas
        uint level;
        uint score;
    }

    mapping(uint => User[]) mulUsers;

    function addUsers (string _name, uint _level, uint _score) public {
        User memory user = User(_name,_level,_score); 
        mulUsers[_level].push(user);
    }

    // Return array of bytes32 as solidity doesn't support returning string arrays yet
    function getUsers (uint _level) public view returns (bytes32[] users)
    {
        uint length = mulUsers[_level].length;
        users = new bytes32[](length);

        for(uint i = 0; i < length; i++)
        {
            users[i] = stringToBytes32(mulUsers[_level][i].name);
        }   
    }

    // Make sure that string is not longer than 32 bytes or result will be cut
    function stringToBytes32(string memory source) private pure returns (bytes32 result) {
        bytes memory tempEmptyStringTest = bytes(source);
        if (tempEmptyStringTest.length == 0) {
            return 0x0;
        }

        assembly {
            result := mload(add(source, 32))
        }
    }
}
0

I just refactored your contract code with few changes. Before getting data you have to add to the state variable. Here You have used dynamic array in mapping, hence you would need to push users struct data on particular index.

contract game {

 struct users{
     string name;
     uint level;
     uint score;
 }

 mapping(uint => users[]) mulusers;

 function addusers (uint id, string _name, uint _level, uint _score) public {
     users memory user= users(_name,_level,_score); 
     mulusers[id].push(user);
 }

 function getusers (uint _id) public view returns (string){

    uint index= mulusers[_id].length;

     for(uint i=0; i<index; i++){       
       return mulusers[_id][i].name; 
     }   
  }   
}

This contract should work!

  • 1
    Why would you put return in for cycle? It will just return first index anyway. Edit: nvm, i saw it's in original code too. – Maxpeinas Aug 20 '18 at 6:32
  • so where should i put my return statement beacuse in the loop it will return only one players name – Faheel Sattar Aug 20 '18 at 12:26

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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