on_wire_placed

Place to ask discuss and request the modding support of Factorio. Don't request mods here.
Post Reply
Roblin
Burner Inserter
Burner Inserter
Posts: 15
Joined: Tue Nov 22, 2016 12:06 pm
Contact:

on_wire_placed

Post by Roblin »

context: viewtopic.php?f=25&t=48270

it would be nice to have an event that triggers when a wire is (manually) placed, right now the only event that activates when you place a wire is on_player_cursor_stack_changed, which doesn't let you distinguish between whether you put an item in a chest or whether you made a wire.

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

Re: on_wire_placed

Post by darkfrei »

And also wire cutting with wire tool.

Rseding91
Factorio Staff
Factorio Staff
Posts: 13175
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: on_wire_placed

Post by Rseding91 »

What's the utility of such an event?
If you want to get ahold of me I'm almost always on Discord.

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: on_wire_placed

Post by Mooncat »

Rseding91 wrote:What's the utility of such an event?
Automatically refill wire once you have used one. :)

Player used a wire -> on_wire_placed -> mod inserts a wire to the player.

Or it should be named on_player_placed/used/connected_wire for consistency.

sparr
Smart Inserter
Smart Inserter
Posts: 1327
Joined: Fri Feb 14, 2014 5:52 pm
Contact:

Re: on_wire_placed

Post by sparr »

I was working on a mod that needed to know when circuit connections changed. This would get closer to that.

quyxkh
Smart Inserter
Smart Inserter
Posts: 1027
Joined: Sun May 08, 2016 9:01 am
Contact:

Re: on_wire_placed

Post by quyxkh »

I think a "circuit change counter" readout in LuaCircuitNetwork could be good, update the counter every time a connection is made or broken on that network id and let mods check the change counter against their cache.

Here's my utils/circuit.lua that allows things like `for e in iterate_network(tsrec.stop,'red')`:
code
I only run it when a train leaves a station, and in that use for example the ~2.5ms I think it costs to trace out a circuit connecting a 10-wagon train's buffer chests is truly negligible , but so far as I can tell there's no way to find out whether anything's changed than simply reexamining everything, every time. That cost is for `/c gps=game.player.selected script.on_event(defines.events.on_tick,function(ev) for e in iterate_network(gps,'red') do _=e.get_inventory(1) end end)` with that iterator available.

User avatar
Therax
Filter Inserter
Filter Inserter
Posts: 470
Joined: Sun May 21, 2017 6:28 pm
Contact:

Re: on_wire_placed

Post by Therax »

Here's my use case:

Miniloader uses two invisible inserters, one for each lane of output. To make them circuit controllable, the two inserters need to be wired to each other, but to avoid rendering the circuit connector when the miniloader isn't connected to anything, the inserters can't be connected to each other all the time. In other words, when the miniloader is connected to an entity via circuit, I must script connecting the two inserters to each other and the external network, and vice versa when the miniloader is disconnected.

My current workaround: watch the cursor stack for when the player is holding red/green wire. Then watch the players selection, and poll circuit_connected_entities every tick to see if the player has connected/disconnected the entity. I have to do this on every entity, because the connection isn't created until the player clicks the second entity of a connection. If there were any event for circuit wire use (connect/disconnect) or an event for circuit connection created/removed, I could avoid on_tick polling. I expect polling circuit_connected_entites can't be cheap in larger bases.
Miniloader — UPS-friendly 1x1 loaders
Bulk Rail Loaders — Rapid train loading and unloading
Beltlayer & Pipelayer — Route items and fluids freely underground

User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 582
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: on_wire_placed

Post by Reika »

Rseding91 wrote:What's the utility of such an event?
How about a cached entity in the global table which is to be connected to a vanilla/other-mod entity to read/control, but wants to allow the player to specify connection? Right now the only alternative is to cache the unconnected entity in a "pending" state and recheck connections every few seconds.

I have an entity exactly like this - connect it via wire to a chest and it starts manipulating the chest. Until then, it sits idle, waiting for this connection.

EDIT: More detail here.
Image

User avatar
Therax
Filter Inserter
Filter Inserter
Posts: 470
Joined: Sun May 21, 2017 6:28 pm
Contact:

Re: on_wire_placed

Post by Therax »

Since I needed the functionality for Miniloader, I refactored it into a reusable library, found here. It'll also need a few functions from blueprint.lua, and due to needing to hook so many events, it uses a little event dispatcher in event.lua.

It's about 300 lines of Lua, and uses the following Factorio events:

on_built_entity
on_robot_built_entity
on_player_mined_entity
on_robot_mined_entity
on_entity_died
on_player_cursor_stack_changed
on_selected_entity_changed
on_put_item
on_tick (polled 4 times/second, only when a player is holding a red or green wire over an entity)

Next step is to clean it up, improve the documentation, and see if I can get it into Aforess' stdlib project. This would still be a lot easier with an actual Factorio event, especially to avoid the polling.
Miniloader — UPS-friendly 1x1 loaders
Bulk Rail Loaders — Rapid train loading and unloading
Beltlayer & Pipelayer — Route items and fluids freely underground

lebastaq
Manual Inserter
Manual Inserter
Posts: 1
Joined: Sat May 11, 2019 1:15 pm
Contact:

Re: on_wire_placed

Post by lebastaq »

Hi Therax,

I'm sorry to post on this old thread, but I can't contact you by message on Github nor on the forums.

I'm making a mod that changes electricity management, to make it more realistic.
Each entity has a voltage_in, voltage_out value.

To limit the number of computations, I wish to compute these values only on electric network changes - eg. when entities are connected or disconnected to a network.

While searching around I found your code on github and this thread. Using your lualib would be most useful as it would save me some time and errors (I'm new at modding).

Can I use your library ?

Thanks
Best regards

User avatar
Stringweasel
Filter Inserter
Filter Inserter
Posts: 310
Joined: Thu Apr 27, 2017 8:22 pm
Contact:

Re: on_wire_placed

Post by Stringweasel »

+1

My use case is preventing players from connecting power poles with copper cable in Fluidic Power, by destroying the connection as soon as it's made. (The mod expects only have pipe connections between poles, and not electric connections as well)
Alt-F4 Author | Factorio Modder
Mods: Hall of Fame | Better Victory Screen | Fluidic Power | Biter Power | Space Spidertron | Spidertron Dock | Weasel's Demolition Derby

Post Reply

Return to “Modding interface requests”