Replacing an entity with a related entity based on adjacent entities?

Place to get help with not working mods / modding interface.
Post Reply
User avatar
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Replacing an entity with a related entity based on adjacent entities?

Post by kirazy »

This being an idea it's not really fully formed with all the edge cases, but:

In Bob's mods he has 11 pipe variants making for 12 total. He has three different valves.

My idea is to (using scripting?) swap out the valve that you place with one built on the appropriate pipe graphic for the pipes adjacent to the valve.

My thought was to create hidden entities with the appropriate underlying pipe graphic, and use a script to replace the valve with the one appropriate to the pipe it is connecting to (subject to checks).

The idea is that adding 33 additional valves just to make them match aesthetically is excessive.

I know next to nothing about working with control.lua and scripting, but I was imagining needing to:
a) watch for valves being built
b) watch for pipes being built
c) on load or on mod config change cycle through all the valves in the world and update (is there a way to do that once in a save?)

I mean this sounds like it'd be a performance sink for a minor aesthetic concern, but I'd like to at least explore the idea.

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

Re: Replacing an entity with a related entity based on adjacent entities?

Post by eradicator »

There's no real question in your post. What exactly do you need "modding help" with?

Replacing entities with other entities in on_built* is done by many mods. It is neither difficult nor performance intensive and being on_built would have no permanent performance cost. You'll have to properly define the prototypes to make blueprints and the pipette (Q) work, but that's about it. The initial one-time replacement can be done in a migration. How much time that costs depends on the base ofc (i imagine <30 seconds even for megabases). But if cost is a concern just don't do it for old valves.

What do you intend to do if there's more than one pipe type attached to the valve though?
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
kirazy
Filter Inserter
Filter Inserter
Posts: 416
Joined: Tue Mar 06, 2018 12:18 am
Contact:

Re: Replacing an entity with a related entity based on adjacent entities?

Post by kirazy »

eradicator wrote:
Fri Mar 27, 2020 7:59 pm
There's no real question in your post. What exactly do you need "modding help" with?
...my bad. I thought I included that but it fell out in a rewrite. I don't know how to do this or where to start to make sense of working in control.lua.

Valves are directional entities with four directions, so I want to check in front/behind for pipe type. If it's a supported pipe, rank by material and assign a valve pipe material based on the highest rank entity. I've only worked in startup phase so don't know how to do this. :D Edit: They also are rotatable, so how do you check for that?
eradicator wrote:
Fri Mar 27, 2020 7:59 pm
Replacing entities with other entities in on_built* is done by many mods. It is neither difficult nor performance intensive and being on_built would have no permanent performance cost. You'll have to properly define the prototypes to make blueprints and the pipette (Q) work, but that's about it. The initial one-time replacement can be done in a migration. How much time that costs depends on the base ofc (i imagine <30 seconds even for megabases). But if cost is a concern just don't do it for old valves.
Ah that's good to hear.

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

Re: Replacing an entity with a related entity based on adjacent entities?

Post by eradicator »

Your most important resource is: https://lua-api.factorio.com/


First you'll need an event handler (haha :P). There are several events that you'll want to listen which you can hopefully deal in with a single handler. For the beginning i'd recommend first checking for "if pipe_names[event.created_entity.name]", you can worry about event filters for performance gains later. (pipe_names being a table {pipe_name = true}).

You can get the pipes connected to the valve via LuaEntity.neighbours which should give you an array of fluid boxes in the same order they're defined in the prototype, so if in the prototype all directions have the "front" pipe at i.e. the first position you should be able to ignore the actual direction of the entity.

Code: Select all


local my_handler_function(event)
  --dostuff
  end

script.on_event({
  defines.events.on_built_entity,
  on_robot_built_entity,
  script_raised_revive,
  },
  my_handler_function)
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.

Post Reply

Return to “Modding help”