Page 1 of 1

[2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 7:50 am
by kelianmao
on_built_entity has buggy behavior when pairs of underground belts are built using the new 'smart' belt logic, especially when one of the undergrounds is destroyed.

A short debug print function in control.lua:

Code: Select all

local function on_built_entity(event)
  game.print(event.tick)
  game.print(event.entity.name)
  game.print(event.entity.position)
  game.print(event.entity.belt_to_ground_type)
  -- event.entity.destroy()
end
script.on_event(defines.events.on_built_entity, on_built_entity, {{filter="type", type="underground-belt"}})

When the pair of undergrounds is built, you expect this script event to trigger twice. But instead, you get one merged trigger of the event, somehow printing the event once but both the belts separately.
on_built1.png
on_built1.png (89.58 KiB) Viewed 654 times

It gets even weirder when you add an entity.destroy() call, where it seems to bug out the second underground belt's construction, and the response is incomplete.
on_built2.png
on_built2.png (86.13 KiB) Viewed 654 times
Somehow now only the position gets printed, and the belt_to_ground_type of the second underground is gone. Since you don't get the info that the second belt is an output belt, belt directions are all wrong.

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 8:04 am
by Stringweasel
This is most likely just the print function skipping duplicate messages to prevent spamming. Try setting `skip = defines.print_skip.never` in the `print_settings`. https://lua-api.factorio.com/latest/cla ... html#print

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 8:15 am
by kelianmao
Found further bugginess. Here are 3 cases, built right to left.
on_built3.png
on_built3.png (1.37 MiB) Viewed 643 times
- First one (right), built manually, you see the two triggers, and the input/output are correct.
- Second (middle), auto-built from the bottom, ie forward, the input/output match the manual placement.
- Third (left), auto-built from above, ie against the direction of the belt, the printed input/output directions are wrong. The y=-22.5 belt should be input, and vise versa.

It looks like there might be manual logic (in cpp?) that is correcting the direction of the underground belts, but it is happening after the on_built_entity event has already fired.

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 8:21 am
by kelianmao
Stringweasel wrote: Wed Oct 30, 2024 8:04 am This is most likely just the print function skipping duplicate messages to prevent spamming. Try setting `skip = defines.print_skip.never` in the `print_settings`. https://lua-api.factorio.com/latest/cla ... html#print
Ok, indeed it was skipping duplicate messages. But the issue instead is that both underground belts (with .destroy()) are being placed as input belts, hence the deduplication triggering. So it is still buggy (I originally noticed the issue when using the fields, the printing was just to debug).
on_built4.png
on_built4.png (128.74 KiB) Viewed 641 times

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 8:24 am
by Stringweasel
kelianmao wrote: Wed Oct 30, 2024 7:50 am A short debug print function in control.lua:
Did you add the print settings I mentioned? You're basing your assumptions on incomplete data.

And might I suggest:

Code: Select all

script.on_event(defines.events.on_built_entity, function(event)
  game.print(serpent.block(event), { skip = defines.print_skip.never })
end, {{filter="type", type="underground-belt"}})
Or even better, step through the code using the Factorio Modding Toolkit extension in VSCode.

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Wed Oct 30, 2024 8:29 am
by kelianmao
Yes, the last screenshot is with never skip. Besides, I found the bug when using the fields for other functions, not when printing, I only started printing when I was getting unexpected behaviour.

Re: [2.0.11] Buggy on_built_entity event for 'smart' underground belt placement

Posted: Fri Nov 01, 2024 8:26 am
by Rseding91
Thanks for the report. This is working correctly, until both belts are fully built the input vs output type of the belts are not fully configured.