#Weird Bug when adding Points to a player

1 messages · Page 1 of 1 (latest)

opal brook
#

Hello! I've currently got into a problem. I'm coding a gamemode, where you have to kill someone or kick the other one from the platform.
I've tried an point system, where the first player that reached 5 points win.
Problem is: If one player kills the other or kicks the other player from the platform 2 times in a row, then the other player kills that player that knocked him 2 times of the platform, the guy that already kicked him twice will get the point, instead of the guy, that killed him first time.

I don't know why. Here is my code:
Listener (if player falls of the platform): https://hastebin.com/tucevunocu.less
GameManager (adding Point to player): https://hastebin.com/jebabanobe.csharp
GameManager detecting Enemy / getting Enemy: https://hastebin.com/asekicijul.kotlin

#

I know my explanation is weird, here in easier:
Basically I have to players: Player1 and Player2.
They fight against each other.
Player1 kills Player2 two times in a row.
Player1 gets 2 points.
Then Player 2 kills player1.
Instead of player2 getting the point player1 gets the point.
But when Player2 kills player1 again he gets the point.

reef salmon
#
    return enemy;
}```

Don't compare strings (or objects that aren't enums in general) with !=. Use .equals() instead.

https://www.baeldung.com/java-comparing-objects
Here you can read about that.
It's a common issue among beginners or sometimes in the flow of coding you can easily use != instead of .equals() by accident even as a more experienced developer.
Baeldung

Learn about comparing objects in Java.

#

In short:
== and != compare whether they are the exact same instance of an object (same pointer).
.equals() compares whether the content of the objects is equal.

That's why it sometimes works for you while it doesn't always work the way you'd expect it to work