Request getting certain slot in an inventory

Post Reply
HoneyFox
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu May 08, 2014 9:09 am
Contact:

Request getting certain slot in an inventory

Post by HoneyFox »

Would be good if we can have something like this:
Lua/InventorySlot slot = game.player.getinventory(...).getslot(slot_Index)
Lua/InventorySlot will have:
(read/write) Lua/ItemStack itemStack: return the item stack stored in this slot, return nil if nothing inside.
(read/write) table<string> slotReservedForItems: currently only used in Quickbar, empty table if it's not reserved for any type of item, can have multiple itemnames inside the table to allow any one of them to be inside this slot.
P.S. This idea can actually be extended to use in player's main inventory/boxes/cars/wagons' inventories as well so that we can ... plan what types of goods a wagon is for, e.g. half of the slots for iron-plates and half of the slots for copper-plates.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Request getting certain slot in an inventory

Post by FreeER »

yeah, I just ran into this helping McSpiffy with the C&C harvester... needed to find out what the refinery had in it so that it could dynamically output it onto the ground but the only way to do that (since getinventory(1) returns userdata...) was a hackish for loop that iterates over getcontents instead and just gets the first provided index/value and then breaks out of the loop.
edit: obviously my example didn't involve anything complex (like filters) but this capability would eliminate an unneeded for loop

HoneyFox
Burner Inserter
Burner Inserter
Posts: 12
Joined: Thu May 08, 2014 9:09 am
Contact:

Re: Request getting certain slot in an inventory

Post by HoneyFox »

FreeER wrote:yeah, I just ran into this helping McSpiffy with the C&C harvester... needed to find out what the refinery had in it so that it could dynamically output it onto the ground but the only way to do that (since getinventory(1) returns userdata...) was a hackish for loop that iterates over getcontents instead and just gets the first provided index/value and then breaks out of the loop.
Well I don't quite get what you said, if you want to check what's in a refinery shouldn't it be easier to use the current getcontents() instead of iterating slots one by one?

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Request getting certain slot in an inventory

Post by FreeER »

HoneyFox wrote:Well I don't quite get what you said, if you want to check what's in a refinery shouldn't it be easier to use the current getcontents() instead of iterating slots one by one?
Well I wanted to loop through the inventory slots and find what the first slot was that actually had an item (so it was out put from first slot to last), getcontents is what I ended up using, but I still needed a for loop with it because it returns a table in the form of {"iron-ore"=20, "coal" = 10} etc, and since I didn't have any idea of what those indexes are at the time of calling... This is what I used

Code: Select all

local item
for name, count in pairs(...getcontents()) do
  item = {name=name, count=count} -- find out what is inside the refinery so it can be output
  break
end
edit: I have been informed by rk84 that the lua 'next' function would have allowed me to do what I did with the for loop (just get the first non-nil name/count values).

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Request getting certain slot in an inventory

Post by drs9999 »

I just (accidently) found out that you have at least read access to specific inventory slots :D

E.g. use the following command while hovering over a chest:

Code: Select all

game.showmessagedialog(serpent.block(game.player.selected.getinventory(1)))
It will print a "content-table" with the slot-indices as keys and itemstacks as values (or nil if slot = empty).

You can also read a specific slot-index (here the second slot) with:

Code: Select all

game.showmessagedialog(serpent.block(game.player.selected.getinventory(1)[2]))
I wasn't able to insert something into a specific slot, but I didn't took much effort into it, so it might be possible as well...

drs9999
Filter Inserter
Filter Inserter
Posts: 831
Joined: Wed Mar 06, 2013 11:16 pm
Contact:

Re: Request getting certain slot in an inventory

Post by drs9999 »

P.S. Just for completion:
You can also use the size()-operator (or whatever it's calles) "#" to get the inventory-size

E.g.

Code: Select all

game.player.print(#game.player.selected.getinventory(1))

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Request getting certain slot in an inventory

Post by FreeER »

drs9999 wrote:I just (accidently) found out that you have at least read access to specific inventory slots :D
yep, I actually knew that at the time (don't ask why I didn't mention it here, I have no idea) but was running into an issue with using it
cant remember but
drs9999 wrote:You can also use the size()-operator (or whatever it's calles) "#" to get the inventory-size
Oh that's nice to know (PiL calls it the 'length' operator btw)

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

Re: Request getting certain slot in an inventory

Post by Rseding91 »

FreeER wrote:yep, I actually knew that at the time (don't ask why I didn't mention it here, I have no idea) but was running into an issue with using it
cant remember but

I'm not sure if you're still looking to fix this but I had this same issue and worked around it by using this method:

Code: Select all

for n = 1, #game.player.getinventory(2) do
slotContents = game.player.getinventory(2)[n]
end
If you want to get ahold of me I'm almost always on Discord.

User avatar
FreeER
Smart Inserter
Smart Inserter
Posts: 1266
Joined: Mon Feb 18, 2013 4:26 am
Contact:

Re: Request getting certain slot in an inventory

Post by FreeER »

Rseding91 wrote:I'm not sure if you're still looking to fix this
Not anymore, I actually gave the code I used to get around this in an earlier post, and was informed by rk84 that the lua 'next' function (used by pairs) would have been more elegant than my solution :)
I chose to use the contents instead of the inventory slots since then I didn't have to check to see if that slot was empty or not; and for my purpose I didn't need the slot itself for anything, just the item(s) in it.

edit: as for the OP since they haven't posted in this topic in quite a while...they've either had their issue 'solved' or gave up :P

Post Reply

Return to “Implemented mod requests”