I'm taking my first dive in blockchain and solidity. I'm trying to make a simple dragon trading and breeding game. In the breeding function I'm trying to take all unique elements of the two dragons taking part in the breeding, using this very simply method. But I get this error that I can't understand. Everything I search says just to use .length to get the length of the array but it doesn't seem to work.
contract Dragon
{
enum Element { Fire, Water, Air, Earth }
string public name;
Element[] public element;
uint public rarity;
constructor (string memory _name, Element[] memory _element, uint _rarity) public
{
name = _name;
element = _element;
rarity = _rarity;
}
function breed (Dragon a, Dragon b) public view returns(Dragon)
{
Element[] memory newElement;
for (int i = 0; i < a.element.length; i++) <-- the problem is here
{
newElement.push(a.element[i]);
}
for(int i = 0; i < b.element.length; i++) <-- I guess it is here as well but the compiler gets stuck on the one above first
{
for (int j = 0; j < newElement.length; j++)
{
if (b.element[i] != newElement[j])
{
newElement.push(b.element[i]);
}
}
}
string memory newName = "";
for(int i = 0; i < newElement.length; i++)
{
newName += newElement[i] + "-";
}
newName += "Dragon";
return new Dragon(newName, newElement, a.rarity+b.rarity);
}
}
a.elementis a function since a is a Dragon contract. @cgeopapa You don't have direct access to an array length from another contract. You have to create a getter that returns the length. – Ismael Jun 09 '21 at 01:42