Page 1 of 1

neat requester chest

Posted: Thu Oct 15, 2015 11:29 am
by judos
Hello there,

I would like to make some neat logistics items:
- Neat requester chest:
When placed it searches for assembling machines in 3x3 radius. For all assembling machines found it takes the ingredients for their recipes needed and sets these items as filter to request.
I already have this code:

Code: Select all

require "defines"
require "basic-lua-extensions"

game.on_event(defines.events.on_built_entity, function(event)
	if event.created_entity.name == "logistic-chest-requester-neat" then
		debug(serpent.block(event))
	end
end)
Can you help me how to set the requested items for this chest ? I'm a programmer but I don't know the functions which are available to do this.

Thank you for your response!

Re: neat requester chest

Posted: Thu Oct 15, 2015 7:25 pm
by jorgenRe
Firstly there is the beloved wiki:
https://forums.factorio.com/wiki/inde ... quest_slot

Secondly when i create mods i like to see how others did things so i would advice you to check out my mod pneumatic tubes:

Now heres what i know works:
Make requests on the requester

Code: Select all

  if found == 0 then
  slotThere = 0
    for slot = 1, 10 do 
      slotInfo = supplier.get_request_slot(slot)
	  if slotInfo ~= nil then 
	    if slotInfo["name"] == name and slotThere == 0 then
	      slotThere = 1
        end
      elseif slotInfo == nil then
   	    --Set where last empty slot was found!
	    emptySlot = slot
	  end
	end
	   if slotThere == 0 then
	     supplier.set_request_slot({name = name, count = need}, emptySlot)
		 slotThere = 1
	   end
  end
PS just change supplier with the selected entity.

Now here is the part im not so good with:
game.player.print(game.player.selected.recipe.name) -- prints the recipe of the selected assembling machine/furnace
From
https://forums.factorio.com/wiki/inde ... Lua/Recipe

Other than that you can do this to check what the ingredients of an entity is using the:

Code: Select all

/c ingredientsTable = game.player.selected.recipe.ingredients 
which is a table of the ingredients
so if you select an assembler and selects position one in the ingredient table and then the name you can do it like so:

Code: Select all

/c game.player.print(game.player.selected.recipe.ingredients[1].name)
which will return the name of the first ingredient

PS its nice to read something from 4 hours into the future :lol: !

Re: neat requester chest

Posted: Thu Oct 15, 2015 9:27 pm
by judos
Ok nice everything works :) The wiki was a great hint. I didn't really know how to find stuff there before.
Code, how I did it
Now another question arose out of this:
I created a new item "logistic-chest-requester-neat" but actually I want an entity for it as well. My idea is to just reuse the standard requester chest entity.
So right now I set in the items.lua:

Code: Select all

place_result = "logistic-chest-requester",
But obviously then in the "game.on_event(defines.events.on_built_entity," I only get events when the standard chest is built.
Now in the wiki I found the method "on_put_item - Called when players uses item to build something. Called before on_built_entity."

How would you connect those to only execute my function, when the modded chest is placed? Is it save to use a global variable? Or is it better to define my custom entity (the same as the default chest) ?

Thank you for your help

Re: neat requester chest

Posted: Fri Nov 13, 2015 3:31 pm
by SirRichie
Not sure if this might be the wrong question to ask, but why do you place a different entity?

What you would typcially do is create an entity (see the modding tutorial on how to do that in principle) that is based on the chest but has a different name.

Re: neat requester chest

Posted: Fri Nov 13, 2015 8:11 pm
by judos
Hi SirRichie,

Well actually I did it the following way now:

Code: Select all

local neatSmartInserter = deepcopy(data.raw["inserter"]["smart-inserter"])
neatSmartInserter.name = "neat-smart-inserter"
neatSmartInserter.minable.result = "neat-smart-inserter"
data:extend({	neatSmartInserter })
Where can I find your tutorial? Is this a good approach I took ?

Cheers

Re: neat requester chest

Posted: Tue Dec 01, 2015 6:12 am
by SirRichie
It is not my tutorial, it is in the wiki: https://forums.factorio.com/wiki/inde ... g_Tutorial
The tutorial is not up to date regarding the modding API, but it is great for getting the idea (focus on Chapter 4).

Re: neat requester chest

Posted: Tue Dec 01, 2015 9:48 pm
by judos
Very good thanks for the link.

I did a first release already on github. See: Github Releases - NeatLogistics