616
submitted 1 year ago by [email protected] to c/[email protected]
you are viewing a single comment's thread
view the rest of the comments
[-] [email protected] 159 points 1 year ago
public class GameManager : MonoBehaviour
{
    public bool EnableHighContrast;
    public bool PlayerWon;
    public float PlayerUnitsMoved;
    public int PlayerDeathCount;
    public float PlayerHealth;

    public void PlayerTakeDamage(float damage)
    {
        PlayerHealth -= damage;
        if (PlayerHealth < 0)
        {
            PlayerDieAndRespawn();
        }
    }

    public void PlayerDieAndRespawn()
    {
        return;
    }
}

I couldn't contain myself.

[-] [email protected] 57 points 1 year ago

Should it be

PlayerHealth <= 0

?

Otherwise the player could have 0 health and not die? I’m sleep deprived so forgive me if I’m wrong

[-] [email protected] 5 points 1 year ago* (last edited 1 year ago)

I know this is /c/Progammerhumor, but I wanted to pull on this thread a little bit for my own edification. I'm a Python guy and have been a while, but I've dabbled in other languages. The screenshot says "MonoBehaviour" which makes me assume this is mono or a .Net-like language (you know what happens when you assume).

If your player health is a float, would mono or .Net have an issue comparing the float with integer zero "0"? I mean, it seems like floating point precision may make it impossible for it to ever "equal" integer zero, but it also seems like the code isn't accounting for that precision error.

Am I overthinking this?

[-] [email protected] 1 points 1 year ago

As a noob in unity and programming, my understanding is that MonoBehavior only means that this script has to be attached as a component to a game object to function. And has no other meaning - but correct me if I'm wrong please.

load more comments (2 replies)
load more comments (8 replies)
load more comments (15 replies)
this post was submitted on 28 Mar 2024
616 points (98.1% liked)

Programmer Humor

37105 readers
183 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS