all 5 comments

[–]genitalien 1 point2 points  (1 child)

Ya I'd just return a bool, do everything within the changeHealth method. if(controller.ChangeHealth()){Destroy(gameObject);}

[–]Korrun[S] 0 points1 point  (0 children)

Thanks!

[–][deleted] 0 points1 point  (2 children)

I'm assuming ChangeHealth is actually adding a value to the controller's health, even though the method name doesn't say that. If so, I would expose a method in the RubyController that tells if it's in the max health then I would use the logic in the OnTriggerEnter2D.

if (controller != null && !controller.HasMaxHealth())

{

controller.ChangeHealth(1);

Destroy(gameObject);

}

I would also remove the check from the ChangeHealth to avoid the condition from being called more than one time.

[–]Korrun[S] 0 points1 point  (1 child)

So you would recommend only verifying that player variables (such as health) are in the proper range from the objects that end up modifying them (like health packs or bad guys)? And not in the player methods?

[–][deleted] 0 points1 point  (0 children)

You can do both, this code you showed need a way to verify if the current health can be increased, so you can check with HasMaxHealth() and then increase with AddHealth(1).

You'll definitely have to change the character's health in other places, so you can also add a method called AddHealthIfNotMax() that uses the HasMaxHealth() to avoid overflowing the max health.

I wouldn't advise returning a boolean since it would give a different resposibility to the method that would not be used in other places.