Page 1 of 1

Detecting when a player replaces an entity

Posted: Sun Dec 08, 2019 6:36 am
by MewMew
Right now, it seems that it throws entity built and mined event.
Is there a way to determine when a player manually replaces/upgrades an entity, like a transport-belt?

Re: Detecting when a player replaces an entity

Posted: Sun Dec 08, 2019 3:00 pm
by darkfrei
You get two events simultaneously, just check them.
https://mods.factorio.com/mod/FillingBeltsUpdater

Re: Detecting when a player replaces an entity

Posted: Sun Dec 08, 2019 6:58 pm
by MewMew
don't understand how that would help

Re: Detecting when a player replaces an entity

Posted: Sun Dec 08, 2019 7:01 pm
by DaveMcW
What feature are you trying to build by detecting this event?

Re: Detecting when a player replaces an entity

Posted: Mon Dec 09, 2019 1:11 pm
by MewMew
it is about xp gain from placing entities, but if you fast replace it goes bonkers, so it needs to ignore replacing of entities

Re: Detecting when a player replaces an entity

Posted: Mon Dec 09, 2019 3:15 pm
by DaveMcW
I would subtract xp for each entity mined. Keep a xp buffer that is only given to the player once per tick, so fast replace ends up with zero net xp. Also you can add a check to ensure actual xp never goes down.

Re: Detecting when a player replaces an entity

Posted: Mon Dec 09, 2019 4:16 pm
by MewMew
That does not do what i would like it to do.

Re: Detecting when a player replaces an entity

Posted: Tue Dec 10, 2019 1:46 am
by Honktown

Code: Select all

local tick = 0
local last_entity = nil
...
function mything(event)
  if last_entity ~= nil then
    -- probably need last_entity.valid because something doesn't exist after being mined
    if tick == event.tick and last_entity.valid and (event.entity.position[1] == last_entity.position[1] and event.entity.position[2] == last_entity.position[2]) then
      return
    end
  end

  ...do normal stuff...

  tick = event.tick
  last_entity = event.entity

end
I don't know if the positions would be given as [1], [2] or x,y so try it and see if you get errors. Also, I don't know how events process if multiple entities are placed/mined in the same tick (is it mined mined mined placed placed placed, or mined placed mined placed mined placed?). The former would need a table, so you keep track of all things mined at a time, and check if any of them were a thing we built over.

Re: Detecting when a player replaces an entity

Posted: Tue Dec 10, 2019 4:21 pm
by MewMew
having a hashtable that saves every entity built that can be replaced, is extremely bloated way to do such a small thing

Re: Detecting when a player replaces an entity

Posted: Tue Dec 10, 2019 4:26 pm
by eradicator
Store all deconstructions in a tick-aware cache, if a build event has the same position and fast_replaceable_group as an entity in the cache you know it's a replacement action. If the cache tick index is not the current tick at the start of any event delete the cache. I.e. you only ever need to store a single tick worth of deconstruction events.

Re: Detecting when a player replaces an entity

Posted: Tue Dec 10, 2019 10:11 pm
by MewMew
it may be enough to save only the position of the last placed entity + tick and compare it with the entity position that is mined next
not sure if this will give the correct result always, got to try

Re: Detecting when a player replaces an entity

Posted: Wed Dec 11, 2019 9:43 am
by MewMew
there is no way to do this properly, it seems, only found a way to keep track of some entities that were placed in the last x amount of ticks and compare them to the ones mined, but that is quite wonky