- Home /
short assigned to negative of another short acting like int
So I have this loop here.
Despite every variable being a short (including enemyStats.vision), the debug is calling "Cannot implicitly convert type int' to
short'. An explicit conversion exists (are you missing a cast?" on the middle for loop's initiator.
I managed to fix it by replacing the initiator with counter = (short)-radius
but I can't figure out why I should need to in the first place. Anyone know what's going on?
`TileProperties tileToCheck;
for(short radius = 1; radius < enemyStats.vision; radius++) // For each radius up to the vision radius of the enemy
{
for(short counter = -radius; counter < radius; counter++)
{
for(short counter2 = radius; counter2 > -radius; counter2--)
{
tileToCheck = gameManager.allTiles[currentTile.col + counter, currentTile.row + counter2];
}
}
}`
Answer by FortisVenaliter · May 12, 2015 at 09:14 PM
It's because of the way shorts work, if I recall correctly. The maximum positive value is one less than the minimum negative value, so when you negate it, there's a possibility of it overflowing, which would constitute a 'bad cast' which is why it requires you to make it explicit.
See here for more info.