161 points
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.

permalink
report
reply
58 points

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

permalink
report
parent
reply
27 points

You are correct.

permalink
report
parent
reply
54 points

Counting this meme as my first FOSS contribution

permalink
report
parent
reply
27 points

Open up ticket first, please. Thanks Codemonkey.

permalink
report
parent
reply
12 points
*

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.

permalink
report
parent
reply
5 points
*

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?

permalink
report
parent
reply
7 points

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?

permalink
report
parent
reply
1 point

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.

permalink
report
parent
reply
-4 points

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.

permalink
report
parent
reply
35 points

Yay, escaped the fight with 0 health!

permalink
report
parent
reply
16 points

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).

permalink
report
parent
reply
5 points

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

permalink
report
parent
reply
10 points

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

permalink
report
parent
reply
15 points

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

permalink
report
parent
reply
6 points

The doctor prescribed “getting good”

permalink
report
parent
reply
5 points

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

permalink
report
parent
reply
113 points

Great. Now that my code is self-documenting it is somehow also not legible?

Make up your damn minds, peeps!

permalink
report
reply
23 points

Is this… clean code ? 💁‍♂️ 🦋

permalink
report
parent
reply
102 points

gestures at butterfly this code

Is this self-documenting code?

permalink
report
reply
40 points

I genuinely believe something like this is what some of my professors wanted me to submit back in school. I once got a couple points off a project for not having a clarifying comment on every single line of code. I got points off once for not comment-clarifying a fucking iterator variable. I wish I could see what they would have said if I turned in something like this. I have a weird feeling that this file would have received full marks.

permalink
report
parent
reply
12 points

Did you have my professor for intro to C? This guy was well known for failing people for plagiarism on projects where the task was basically “hello world”. And he disallowed using if/else for the first month of class.

permalink
report
parent
reply
6 points

Reminds me of an early Uni project where we had to operate on data in an array of 5 elements, but because “I didn’t teach it to everyone yet” we couldn’t use loops. It was going to be a tedious amount of copy-paste.

I think I got around it by making a function called “not_loop” that applied a functor argument to each element of the array in serial. Professor forgot to ban that.

permalink
report
parent
reply
71 points

I would take this over int a; anyday

permalink
report
reply
33 points

Amateur! Ints should be called i, j or k.

permalink
report
parent
reply
28 points

Cmon now its illegal to use them anywhere other than iterators

permalink
report
parent
reply
4 points

I sometimes use n as my loop variable (⌐■_■)

permalink
report
parent
reply
3 points

This is the way

permalink
report
parent
reply
18 points

int IntegerThatWillBeUsedToIterateOverAListOfItemsAndModifyThem2

permalink
report
parent
reply
71 points

This is something that can easily get refactored, because the purpose of alia the variables is right there in the name. This is way better that spending three days to try to figure out what the purpose of var1 is.

permalink
report
reply
7 points

Nah, refactoring this would be a bitch. Your function name contains everything that happens in the function. Which means if you add something to it, you also have to change the name of the function. So CallThisWhenThePlayerTakesDamageAndIfThePlayerHealthIsLessThanZeroThenAlsoTheyDie would have to go to something like CallThisWhenThePlayerTakesDamageAndIfThePlayerHealthIsLessThanZeroThenAlsoTheyDieAndIncrementTheTotalDamageTakenCounter if you added something else.

permalink
report
parent
reply
11 points

IDE renames all references, no issue

permalink
report
parent
reply

oh such hope

in a week IntegerThatTracksOneThingForOnePurpose will be an object tracking 30% of the game state and mutated in 15 places without ever being renamed

permalink
report
parent
reply
1 point

Then they change what the function does without updating the name and you misunderstand the code completely.

permalink
report
parent
reply

Programmer Humor

!programmerhumor@lemmy.ml

Create post

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

Rules:

  • Posts must be relevant to programming, programmers, or computer science.
  • No NSFW content.
  • Jokes must be in good taste. No hate speech, bigotry, etc.

Community stats

  • 3.7K

    Monthly active users

  • 1.5K

    Posts

  • 35K

    Comments