Challenge

I would say that player.health is acceptable (barring public variables, of course!), but not only would player.gun.ammo.reoundsLeft be potentially dangerous, it also wouldn’t work in this code. gun & ammo are undefined, so there is no way that roundsLeft can be accessed. You would need a FindObjectOfType() (or similar) in player and an ammo = new Ammo() in Gun. I would then chain a PrintRoundsLeft() method (one in Player, one in Gun) which ends up with print(ammo.roundsLeft.ToString()) and begins with player.PrintRoundsLeft() in the GunUI.

I would then start to second guess myself on cohesion/coupling. :slight_smile:

2 Likes

It would work fine.

In the right panel:
The player has a gun defined [player.gun]
which in turn has ammo defined [player.gun.ammo]
which has roundsLeft defined [player.gun.ammo.roundLeft]

A mess but it would work.

1 Like

I think Clive might have been referring to the fact that we don’t ever see Gun or Ammo assigned anywhere, so they’re just sitting in a null state.

player.gun.ammo.roundsleft needs some null checks and that shouldn’t really be the responsibility of the UI.

Privacy & Terms