Access content of trash slots

Place to get help with not working mods / modding interface.
Summoner99
Burner Inserter
Burner Inserter
Posts: 7
Joined: Thu Jun 04, 2020 5:33 pm
Contact:

Access content of trash slots

Post by Summoner99 »

Hello Im fairly new to factorio mod development (but not programming in general). Im looking to make a small mod that interacts with the logistic trash slot. My problem is I'm not sure how to get access to the items in the trash slots

I've figured out how to access the main inventory with `player.get_main_inventory()` but I can't find anything specific to the trash slots on the docs. Maybe I'm just overlooking it?

Side note: Is there a better way to find the max non-nil logistic request slot_index? My solution is just to keep trying until the last 10-ish slots have been nil but that isn't a nice solution

Pi-C
Smart Inserter
Smart Inserter
Posts: 1720
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Access content of trash slots

Post by Pi-C »

Summoner99 wrote:
Wed Aug 07, 2024 4:14 pm
I've figured out how to access the main inventory with `player.get_main_inventory()` but I can't find anything specific to the trash slots on the docs. Maybe I'm just overlooking it?
You can use LuaControl::get_inventory:

Code: Select all

local trash
local character = player and player.valid and player.character
if character and character.valid then
  trash = character.get_inventory(defines.inventory.character_trash)
end
Side note: Is there a better way to find the max non-nil logistic request slot_index? My solution is just to keep trying until the last 10-ish slots have been nil but that isn't a nice solution
You can use LuaForce::character_trash_slot_count for the character trash slots that every player on the force has, and LuaControl::character_trash_slot_count_bonus for the bonus trash slots a given character may have.
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Summoner99
Burner Inserter
Burner Inserter
Posts: 7
Joined: Thu Jun 04, 2020 5:33 pm
Contact:

Re: Access content of trash slots

Post by Summoner99 »

Thank you so much! That will help with exactly what I need to do.

The second point about finding the "max non-nil logistic request slot_index" was specifically referring to the logistic request slots, not the trash slots. Thats fine though. My solution incurs a negligible performance hit so Im not worried about it

Pi-C
Smart Inserter
Smart Inserter
Posts: 1720
Joined: Sun Oct 14, 2018 8:13 am
Contact:

Re: Access content of trash slots

Post by Pi-C »

Summoner99 wrote:
Wed Aug 07, 2024 5:27 pm
The second point about finding the "max non-nil logistic request slot_index" was specifically referring to the logistic request slots, not the trash slots. Thats fine though. My solution incurs a negligible performance hit so Im not worried about it
Ooops, sorry! My mind was set on trash, so I didn't notice you wanted logistics. :-)

Anyway, you can get the index of the last non-nil slot with LuaEntity::request_slot_count. However, you should note that you can't use this with LuaPlayer, but only with a character!

Code: Select all

local char = player.character
if char and char.valid then
  local max_slot = char.request_slot_count
end
Also, note that LuaEntity::request_slot_count will always get the last non-nil slot -- regardless of whether "Personal logistics and autotrash" is active! Therefore, you should probably combine this with LuaControl::character_personal_logistic_requests_enabled:

Code: Select all

local logistics_enabled = player.character_personal_logistic_requests_enabled
local char = player.character
if logistics_enabled and char and char.valid then
  local max_slot = char.request_slot_count
end
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

Post Reply

Return to “Modding help”