Page 1 of 1
[1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 8:11 am
by Deadexow
What did you do?
I simply entered the command
Code: Select all
/c local inv = game.create_inventory(16) local invs = game.get_script_inventories() game.print(invs[1])
in the command box.
What happened?
It only returns
nil
, which does not align with the return value described in your API documentation.
What did you do next?
I entered the command
Code: Select all
/c local inv = game.create_inventory(16) local invs = game.get_script_inventories(inv.mod_owner) game.print(invs[1])
in the command box
What happened?
It only print
Cannot execute command.Error:Unknown mod:core
- What did you expect to happen instead?
I wish to get the inv that I created using create_inventory() through the function get_script_inventories() or others.
Does it happen always, once, or sometimes?
This issue can be reproduced easily by following my steps anytimes.
log-file
save-file
Do I need to provide a save file? This issue can be reproduced easily by following my steps in any savegame.
adds
I am a mod developer from China, and my English is not very proficient.
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 9:34 am
by Loewchen
Deadexow wrote: Thu Jun 20, 2024 8:11 am
I simply entered the command
Inventories created through console commands will be owned by "core".
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 10:20 am
by Deadexow
Regardless of whether I use the command console or a module to call this function, the return value of this function is always an empty table.
Furthermore, when I use the command console command game.get_script_inventories("core"), this function results in an error, as previously demonstrated. Consequently, I am unable to retrieve the inv created by game.create_inventory() through any function again.
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 12:20 pm
by FuryoftheStars
https://lua-api.factorio.com/latest/cla ... nventories
Return values
→ dictionary[string → array[LuaInventory]]
A mapping of mod name to array of inventories owned by that mod.
I mean, I can't be sure, but from your first command, I'd expect game.print(invs[1]) to be nil.
Try a for loop with pairs and see what you get out of that.
Code: Select all
for k, v in pairs(invs) do
game.print("key, value: " .. k .. ", " .. v)
end
(Assuming I didn't typo anything.)
(Edit: I'm wondering if game.print(invs["core"]) is actually what you need? But try the loop, first, before deciding that there is actually nothing there.)
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 11:19 pm
by Pi-C
FuryoftheStars wrote: Thu Jun 20, 2024 12:20 pm
I mean, I can't be sure, but from your first command, I'd expect game.print(invs[1]) to be nil.
Correct.
Try a for loop with pairs and see what you get out of that.
Code: Select all
for k, v in pairs(invs) do
game.print("key, value: " .. k .. ", " .. v)
end
(Assuming I didn't typo anything.)
This wouldn't work. For each mod you'll get an array of inventories (where each inventory is an array of LuaItemStack objects), so game.print() will error with "string expected, got table".
(Edit: I'm wondering if game.print(invs["core"]) is actually what you need? But try the loop, first, before deciding that there is actually nothing there.)
It will work if you loop over the inventories in invs["core"]. Try this on the console:
Code: Select all
/c test = game.create_inventory(16)
invs = game.get_script_inventories()
for i, inv in pairs(invs.core) do
game.print(string.format("%s %s of mod \"%s\", %s slots %s", inv.object_name, i, inv.mod_owner, #inv, tostring(inv.is_empty() and "(empty)" or inv.is_full() and "(full)" or "")))
end
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Thu Jun 20, 2024 11:31 pm
by FuryoftheStars
Pi-C wrote: Thu Jun 20, 2024 11:19 pm
FuryoftheStars wrote: Thu Jun 20, 2024 12:20 pm
Try a for loop with pairs and see what you get out of that.
Code: Select all
for k, v in pairs(invs) do
game.print("key, value: " .. k .. ", " .. v)
end
(Assuming I didn't typo anything.)
This wouldn't work. For each mod you'll get an array of inventories (where each inventory is an array of LuaItemStack objects), so game.print() will error with "string expected, got table".
Yeah, I was wondering about that while writing it. I was posting from my phone and wouldn't have access to the game until, well, now.
So conceivably, running the loop just printing the value of 'k' would have been enough to prove that something was there, or wrapping 'v' with tostring():
Code: Select all
/c local inv = game.create_inventory(16) local invs = game.get_script_inventories() for k, v in pairs(invs) do game.print("key, value: " .. k .. ", " .. tostring(v)) end
- Screenshot 2024-06-20 193025.png (282.3 KiB) Viewed 521 times
Edit: Could also use serpent on 'v':
- Screenshot 2024-06-20 193512.png (1.47 MiB) Viewed 516 times
Re: [1.1.107]The API function get_script_inventories() is not returning any value.
Posted: Sat Jun 22, 2024 9:48 am
by Deadexow
Thank you very much,now I know what the function return:
Code: Select all
{
["mod_name"] = {inv,inv,……},
["mod_name"] = {inv,inv,……},
}