[Solved] Why use missile.GetDamage() instead of just using missile.damage?

I’m not quite sure why we’re supposed to use a method called missile.GetDamage() that returns the damage, instead of just using missile.damage. Is there a specific reason for this? To me it just seems like extra lines of code that we don’t really need, but I might be overlooking something. Can anyone expain this to me? Thanks!

1 Like

Hello @RobvdB, how are you?

The way it is in the game won’t make any difference, but the main reason on my opinion is that you can improve the way it calculates the damage afterwards and you won’t need to restructure the references. For example, you want it to apply modifiers depending on the tag of the enemy and if the damage was critical or not, and then add a random range to the damage. It would be simple to do it within a method without too much change to your code, but it would be harder to make it using just a variable.

I think that many things that they add to those games ain’t because the game would be better if they do, but because they want to teach us good practices

3 Likes

What Joao said, in OOP it’s called Encapsulation.

3 Likes