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.
Request getting certain slot in an inventory
Re: Request getting certain slot in an inventory
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
edit: obviously my example didn't involve anything complex (like filters) but this capability would eliminate an unneeded for loop
Re: Request getting certain slot in an inventory
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?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.
Re: Request getting certain slot in an inventory
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 usedHoneyFox 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?
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
Re: Request getting certain slot in an inventory
I just (accidently) found out that you have at least read access to specific inventory slots
E.g. use the following command while hovering over a chest:
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:
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...
E.g. use the following command while hovering over a chest:
Code: Select all
game.showmessagedialog(serpent.block(game.player.selected.getinventory(1)))
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]))
Re: Request getting certain slot in an inventory
P.S. Just for completion:
You can also use the size()-operator (or whatever it's calles) "#" to get the inventory-size
E.g.
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))
Re: Request getting certain slot in an inventory
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 itdrs9999 wrote:I just (accidently) found out that you have at least read access to specific inventory slots
cant remember but
Oh that's nice to know (PiL calls it the 'length' operator btw)drs9999 wrote:You can also use the size()-operator (or whatever it's calles) "#" to get the inventory-size
Re: Request getting certain slot in an inventory
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 itcant 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.
Re: Request getting certain slot in an inventory
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 solutionRseding91 wrote:I'm not sure if you're still looking to fix this
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