Gm,
I've got a smart contract for 2D board game purposes. There is a struct Unit
struct Unit
{
UnitType unitType;
uint16 x;
uint16 y;
int8 hp;
int8 dmg;
address owner;
bool isTeamLeft;
}
and a mapping
mapping(uint16 => mapping(uint16 => Unit)) public tileToUnit;
the idea of the mapping was to assign it when new Unit is created, and the main purpose is to have ability to check, given x and y coordinates, if provided coordinates are occupied by any unit and if they are - gain access directly to this Unit.
Since units are moving constantly during the game and fight with each other, I need to change their parameters every move. The problem appears when 2 units fight with each other. Take a look at how fight function is called
if(!fight(_unit,tileToUnit[i][j])) //defender survived
and the fight function itself
function fight(Unit storage _attacker, Unit storage _defender) private returns (bool) ///returns true if defender died
{
_defender.hp -= _attacker.dmg;
_attacker.hp -= _defender.dmg;
if (isUnitAlive(_defender)) //defender Survived
{
return false;
}
else
{
liquidateUnit(_defender);
unmapUnit(_defender);
return true;
}
}
The problem is that when we try to reach second unit while calling fight function, we're not able to reach the storage reference via mapping, therefore when later we do _defender.hp -= _attacker.dmg;
, we're not decrementing the actual unit which is saved in storage.
Is there any possibility to solve this issue?