Make chest require energy

Place to get help with not working mods / modding interface.
Post Reply
tat3rjr
Burner Inserter
Burner Inserter
Posts: 10
Joined: Thu Jun 21, 2018 6:55 pm
Contact:

Make chest require energy

Post by tat3rjr »

How do you make a chest require energy to use?

For example have a chest that has a drain of 5kW, but if it does not have any power inserters can not use that chest.

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

Re: Make chest require energy

Post by DaveMcW »

Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.

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

Re: Make chest require energy

Post by eradicator »

DaveMcW wrote: and eats a lot of UPS to run.
If the energy buffer is large enough you can check a lot less often, simply by calculating the minimum time it will be able to run off the buffer alone. Also i don't think you can "deactivate the chest", you'll have to deactivate the inserters.

Definetly not a beginners mod, unless you like a really diabolic challenge :).

tat3rjr
Burner Inserter
Burner Inserter
Posts: 10
Joined: Thu Jun 21, 2018 6:55 pm
Contact:

Re: Make chest require energy

Post by tat3rjr »

DaveMcW wrote:Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.
That seems complicated.

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

Re: Make chest require energy

Post by Rseding91 »

tat3rjr wrote:
DaveMcW wrote:Not easily.

1. Create an electric-energy-source to drain 5kW.
2. Use a control.lua script to add/remove the drainer when a chest is built/destroyed.
3. Use a control.lua script to enable/disable the chest if the drainer is out of energy, firing every tick.

It's a lot of work to set up, and eats a lot of UPS to run.
That seems complicated.
It is :P Chests are not meant to take power because that adds a ton of overhead to moving items around which happens frequently so if you want to make the game do it you have to use extreme workarounds.
If you want to get ahold of me I'm almost always on Discord.

User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: Make chest require energy

Post by bobingabout »

tat3rjr wrote:That seems complicated.
It is, and I would really suggest you not try and do it, however, the fact that someone can tell you the basic steps on how proves that it can be done if you really want it.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.

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

Re: Make chest require energy

Post by darkfrei »

https://lua-api.factorio.com/latest/events.html

Code: Select all

function on_built_entity OR on_robot_built_entity
  Place on this surface in this position new accumulator, that can take energy only
  Add this chest and this accumulator to the global.elements
end

Code: Select all

function on_tick
  if global.elements isn't empty then take the next element 
  if the accumulator in the element has power, then take some energy, if not, then find all inserters about it and disable them. Set flag disabled = true
  if it was disabled, but now we have power, then find all inserters and enable them. Set flag disabled = false
end

Code: Select all

function on_player_mined_entity OR on_robot_mined
  Find this element, delete accumulator, enable inserters, delete this element from global
end

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

Re: Make chest require energy

Post by darkfrei »

Performance optimization: if you have only one chest and it needs every tick 1 kJ, then it needs 60 kW power supply.
If you have two chests, then one tick will be calculated only one of them, take 2 kJ every
second tick and you have also 60 kW.
If you have 7 of them, then just take 7 kJ every 7th tick.

Just check that your accumulator is much bigger than the energy per tick. For 600 chests you are need at least 600 kJ accumulator, then you can calculate 2 chests per tick. Or just make bigger accumulator.

Post Reply

Return to “Modding help”