Detecting when a player replaces an entity

Place to get help with not working mods / modding interface.
Post Reply
User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Detecting when a player replaces an entity

Post 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?

User avatar
darkfrei
Smart Inserter
Smart Inserter
Posts: 2903
Joined: Thu Nov 20, 2014 11:11 pm
Contact:

Re: Detecting when a player replaces an entity

Post by darkfrei »

You get two events simultaneously, just check them.
https://mods.factorio.com/mod/FillingBeltsUpdater

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post by MewMew »

don't understand how that would help

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Detecting when a player replaces an entity

Post by DaveMcW »

What feature are you trying to build by detecting this event?

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post 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

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

Re: Detecting when a player replaces an entity

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

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post by MewMew »

That does not do what i would like it to do.

Honktown
Smart Inserter
Smart Inserter
Posts: 1025
Joined: Thu Oct 03, 2019 7:10 am
Contact:

Re: Detecting when a player replaces an entity

Post 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.
I have mods! I guess!
Link

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post by MewMew »

having a hashtable that saves every entity built that can be replaced, is extremely bloated way to do such a small thing

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: Detecting when a player replaces an entity

Post 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.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post 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

User avatar
MewMew
Long Handed Inserter
Long Handed Inserter
Posts: 55
Joined: Thu May 19, 2016 11:02 am
Contact:

Re: Detecting when a player replaces an entity

Post 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

Post Reply

Return to “Modding help”