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.
Re: Request getting certain slot in an inventory
Posted: Fri May 16, 2014 5:11 am
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
Re: Request getting certain slot in an inventory
Posted: Fri May 16, 2014 9:48 am
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?
Re: Request getting certain slot in an inventory
Posted: Fri May 16, 2014 4:50 pm
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
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).
Re: Request getting certain slot in an inventory
Posted: Thu Aug 14, 2014 8:45 am
by drs9999
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:
drs9999 wrote:I just (accidently) found out that you have at least read access to specific inventory slots
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 can't remember exactly what the issue was but you can see the harvester post I made, and in the next post I made there you can see a comment in the code that
...the for loop I posted
-- on the forums didn't work because the slot is 'userdata'
-- and you can not index userdata...
, of course I could have missed something at the time that was actually causing the error.
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)
Re: Request getting certain slot in an inventory
Posted: Fri Aug 22, 2014 2:31 pm
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 can't remember exactly what the issue was but you can see the harvester post I made, and in the next post I made there you can see a comment in the code that
...the for loop I posted
-- on the forums didn't work because the slot is 'userdata'
-- and you can not index userdata...
, of course I could have missed something at the time that was actually causing the error.
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:
for n = 1, #game.player.getinventory(2) do
slotContents = game.player.getinventory(2)[n]
end
Re: Request getting certain slot in an inventory
Posted: Fri Aug 22, 2014 2:45 pm
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