Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Push an array into struct - Copying of type struct memory[] memory to storage not yet supported

I want to create a Pokémon structure with an array of structs inside.

// SPDX-License-Identifier: GPL-3.0

pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

  struct Pokemon {
    uint256 id;
    string name;
    Ability[] abilities;
  }

  struct Ability {
    string name;
    string description;
  }

  Pokemon[] public pokemons;

  function createPokemon(string memory _name, string memory _abilityNme, string memory _abilityDscription) public {
    uint id = pokemons.length;
    pokemons[id].abilities.push(Ability(_abilityNme, _abilityDscription));
    pokemons.push(Pokemon(id, _name, pokemons[id].abilities));
  }
}

But I am getting the following error:

UnimplementedFeatureError: Copying of type struct PokemonFactory.Ability memory[] memory to storage not yet supported.
like image 202
albbeltran Avatar asked Sep 12 '25 15:09

albbeltran


1 Answers

I adjusted your original smart contract in this way:

// SPDX-License-Identifier: GPL-3.0
pragma solidity >=0.7.0 <0.9.0;

contract PokemonFactory {

  struct Pokemon {
    uint256 id;
    string name;
    Ability[] abilities;
  }

  struct Ability {
    string name;
    string description;
  }

  Pokemon[] public pokemons;

  function createPokemon(string memory _name, string memory _abilityNme, string memory _abilityDscription) public {
    uint id = pokemons.length;
    // NOTE: First I create an 'empty' space in pokemons mapping
    Pokemon storage p = pokemons.push();
    // NOTE: Then after I created this space, I insert the values 
    p.abilities.push(Ability(_abilityNme, _abilityDscription));
    p.id = id;
    p.name = _name;
  }
  
  // NOTE: Function for retrieve Abilities array values for a single pokemon using '_id' parameters for querying the mapping
  function retrieveAbilities(uint _id) external view returns(Ability[] memory){
    return pokemons[_id].abilities;
  }
}

I inserted some comments for understand you each statement

like image 198
Antonio Carito Avatar answered Sep 16 '25 08:09

Antonio Carito