Simple example of ElectricEnergyInterface

Place to get help with not working mods / modding interface.
Post Reply
Ickis
Burner Inserter
Burner Inserter
Posts: 13
Joined: Mon Nov 19, 2018 12:31 pm
Contact:

Simple example of ElectricEnergyInterface

Post by Ickis »

Hello,

I'm wanting my entity to require power that varies with certain conditions. From what I've read, I need to spawn an ElectricEnergyInterface with my entity. I have managed this, but this is showing an accumulator. Again, from what I've read I need to provide a prototype that acts as an invisible entity.

Is there any simple example/mod that uses ElectricEnergyInterface to make an entity require power?

Thank you.

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Simple example of ElectricEnergyInterface

Post by PFQNiet »

The default `electric-energy-interface` is a configurable "cheat" item for playing with energy in creative mode.

You can create your own entity of the `electric-energy-interface` type, and configure it with some sensible default values and invisible graphics. Then, in script, you can set its actual power consumption.

Ickis
Burner Inserter
Burner Inserter
Posts: 13
Joined: Mon Nov 19, 2018 12:31 pm
Contact:

Re: Simple example of ElectricEnergyInterface

Post by Ickis »

Thank you, I don't suppose you know of any example laying around or mod that demonstrates this at all do you to get things going?

PFQNiet
Filter Inserter
Filter Inserter
Posts: 289
Joined: Sat Sep 05, 2020 7:48 pm
Contact:

Re: Simple example of ElectricEnergyInterface

Post by PFQNiet »

I don't have any full examples to hand, but the basic idea is:

Code: Select all

data:extend{
  {
    type = "electric-energy-interface",
    name = "my-entity",
    energy_source = {
      type = "electric",
      buffer_capacity = "...", -- ideally set to 1/60 of the max consumption of the entity - any less and it won't work properly
      usage_priority = "secondary-input",
      drain = "0W"
    },
    pictures = { ... },
    -- minable, collision box, selection box etc.
  }
}
Then you can place this entity and adjust its consumption via script:

Code: Select all

local entity = surface.create_entity{
  name = "my-entity",
  position = ...,
  force = "player"
}
entity.power_usage = 10*1000*1000/60 -- 10MW, the /60 is because power_usage is per-tick
If you don't know the max consumption or it's potentially unbounded, you can set a "reasonable" limit in the `data:extend`, and then dynamically adjust it after setting `entity.power_usage`:

Code: Select all

entity.electric_buffer_size = math.max(entity.electric_buffer_size, entity.power_usage)

Post Reply

Return to “Modding help”