I am building a DApp game and I am experiencing a problem. Let me describe it. I have a struct:
struct NPC {
uint id;
string name;
uint level;
}
Then I have an array that stores all the NPCs:
NPC[] public NPCs;
And mapping that maps from city to NPC:
mapping (uint => NPC[]) public cityNPCs;
When I create new NPC, I push it to both, array and mapping:
function createNpc(string memory _name, uint _level, uint _cityId)
public
returns (bool success)
{
uint newId = NPCs.length;
NPC memory newNpc = NPC(newId, _name, _level);
NPCs.push(newNpc);
cityNPCs[_cityId].push(NPCs[newId]);
return true;
}
The problem is that when NPC struct is updated in the array, the updates are not reflected in mapping and vice versa. So, is there are a way to solve this problem by somehow making array and mapping elements in sync?
Btw, Solidity version:
pragma solidity >=0.5.0;
pragma experimental ABIEncoderV2;