this post was submitted on 28 Mar 2024
615 points (98.1% liked)

Programmer Humor

32380 readers
1361 users here now

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

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 159 points 7 months ago (4 children)
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 7 months ago (5 children)

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] 27 points 7 months ago (1 children)
[–] [email protected] 54 points 7 months ago (1 children)

Counting this meme as my first FOSS contribution

[–] [email protected] 12 points 7 months ago

Holy shit I was there with you sir! With the zeros and stuff

[–] [email protected] 26 points 7 months ago

Open up ticket first, please. Thanks Codemonkey.

[–] [email protected] 12 points 7 months ago* (last edited 7 months ago)

You are correct about it allowing you to have zero health and not die, but whether or not that's the correct behavior will depend on the game. Off the top of my head I know that Street Fighter, some versions at least, let you cling to life at zero.

[–] [email protected] 5 points 7 months ago* (last edited 7 months ago) (2 children)

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] 7 points 7 months ago (1 children)

Floating point errors are a product of how floating points work as a mathematical concept. So they're independent of the programming language and can happen everywhere.

In this case though, I doubt it's a critical issue. So the player "died" when they actually had 0.000000000027 hp left or whatever. Who cares? Do you need to be that precise?

[–] [email protected] 14 points 7 months ago

Hanging on with 1.70E-31 health.

[–] [email protected] 1 points 7 months 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.

[–] [email protected] -4 points 7 months ago

This won't work if you can ever take more than 1 damage. If you were at 1 and received 2 damage you would become invincible. You'd want to do less than or equal to.

[–] [email protected] 34 points 7 months ago (2 children)

Yay, escaped the fight with 0 health!

[–] [email protected] 15 points 7 months ago

Well if you have a "down but not dead" condition then yes, you could escape a fight with 0 health (assuming you have teammates/pawns that can save you).

[–] [email protected] 5 points 7 months ago

This is floating point. We also need to know what happens when you escape with -0.

[–] [email protected] 10 points 7 months ago (1 children)

I called the takeDamage function and my player disappeared: send 'elp everything foobar

[–] [email protected] 15 points 7 months ago (1 children)

Don't worry! this issue will be fixed in the next patch. In the meantime just try not getting hit.

[–] [email protected] 6 points 7 months ago

The doctor prescribed "getting good"

[–] [email protected] 5 points 7 months ago

Too readable, please make each name a paragraph describing its function and how it relates to the other variables/functions around it