Anuken / Mindustry

The automation tower defense RTS
https://mindustrygame.github.io
GNU General Public License v3.0
22.69k stars 2.98k forks source link

UnitCreateEvent is being fired twice when non-t1 unit is being produced #4158

Closed LeoSko closed 3 years ago

LeoSko commented 3 years ago

Platform: Linux server

Build: commit a21cc32b (122 build)

Issue: for https://deranked.ddns.net match recordings I use UnitCreateEvent to detect new unit being made as well as for statistics viewable by spectators. All actions are being recorded by events provided (+some additional ones).

I noticed that t2 units that are being made spawn with 2 exact same events, except the first of them has (x, y) = (0, 0). 2 creation events for same unitId is definetely not expected from my point of view. An example of such match can be found here: https://deranked.ddns.net/match/29232 if you click "Load build order":

Recording of translated UnitCreateEvent from this exact match can be found here: https://pastebin.com/S6rHpCW8 (pulsar's unitType = 6)

Steps to reproduce: add simple listener for UnitCreateEvent which output results, create t2 unit from factory + reconstructor, observe results

Link(s) to mod(s) used: no mods, but I use my server plugin. I have messed with code a little bit, but I'm pretty sure this is the part which I haven't touched (unit spawnings).

Save file: I don't think this is applicable.

(Crash) logs: not a crash


Place an X (no spaces) between the brackets to confirm that you have read the line below.

Anuken commented 3 years ago

There are two things UnitCreateEvents are used for:

Why? Because achievements need to track what kind of units you've built, and in many cases you may be creating a new unit without releasing it from the factory. That still needs to count for, say, the "create every unit type" achievement.

If you only care about new units being added to the map, check unit.isAdded() before handling the event.

This isn't very clean, but it suited my needs when implementing achievements.

LeoSko commented 3 years ago

@Anuken, it seems like UnitCreateEvent is not being triggered when unit is deployed from payload conveyor, which blocks both my feature and the achievement that you mentioned. Should I create a separate issue for that?

Anuken commented 3 years ago

That's not a bug. When a unit is deployed from a payload conveyor, there's no way to know if it's a new unit (just created in a factory) or an older unit that was deposited on the conveyor.

LeoSko commented 3 years ago

@Anuken, thanks for answer. As I understood, there is no way at all to get any event of created T1 unit if it was produced to the conveyor and deployed (aka added) from it. Is that correct?

Anuken commented 3 years ago

Yes. To make the event less ambiguous, I will make it apply only to new unit creation in factories or reconstructors - no deployment.