MOD - Stop Assembly Machine from working

Looking for a mod? Have a review on a mod you'd like to share?
NeXuS
Inserter
Inserter
Posts: 22
Joined: Wed Nov 22, 2017 7:32 am
Contact:

MOD - Stop Assembly Machine from working

Post by NeXuS »

Hi,

and another question I hope you can help me with.

How can I stop an assembly-machine entity from crafting, when a certain global number of the product is reached, even though the machine has all available ingredients?

I don't want to use the signal network for this, just implementing a check against a global variable.
Inside the API I've found something like on_pre_player_crafted_item but this is only the starting event for manual crafting. Is there an event for machine crafting as well?

Best wishes
NeXuS

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

Re: MOD - Stop Assembly Machine from working

Post by darkfrei »

Something like

Code: Select all

script.on_event(defines.events.on_player_crafted_item, function(event)
  local item_stack = event.item_stack 
  if (item_stack.name == 'assembling-machine-1') then
    if not global then global = {} end
    if not global.crafted then global.crafted = 0 end
    global.crafted = global.crafted + 1
    if global.crafted > 5 then
      event.recipe.enabled = false
    end
  end
end)
Not tested. Tested.
See:
events.html#on_player_crafted_item
Concepts.html#SimpleItemStack
LuaRecipe.html#LuaRecipe.enabled

Modding help

NeXuS
Inserter
Inserter
Posts: 22
Joined: Wed Nov 22, 2017 7:32 am
Contact:

Re: MOD - Stop Assembly Machine from working

Post by NeXuS »

Hi darkfrei,

thank you for your fast answer.

I've not tested it yet, but I have a question to your code. ;)

Code: Select all

if global.crafted > 5 then
      event.recipe.enabled = false
    end
If my amount of items drops below five and I re-enable that recipe, do I have to reset the recipe inside the assembling machines or is it just reactivated and works normal without user interaction?

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

Re: MOD - Stop Assembly Machine from working

Post by darkfrei »

NeXuS wrote:If my amount of items drops below five and I re-enable that recipe, do I have to reset the recipe inside the assembling machines or is it just reactivated and works normal without user interaction?
The code must be much complex than above. You can also click spamming on the recipe and get 100 assembling machines instead of 5. You can also clicking with RMB and get 5 items, not only one.

robot256
Filter Inserter
Filter Inserter
Posts: 833
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: MOD - Stop Assembly Machine from working

Post by robot256 »

Normally I use the "connect to logistics network" function on the output inserters to tell it when to stop putting them in a provider chest. This doesn't require wires, and makes sure there are at least a certain number available in the network. Sounds like you might have a different need though.

Post Reply

Return to “Questions, reviews and ratings”