[1.1.107]The API function get_script_inventories() is not returning any value.

Place to get help with not working mods / modding interface.
Post Reply
Deadexow
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jun 14, 2024 1:47 pm
Contact:

[1.1.107]The API function get_script_inventories() is not returning any value.

Post 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
factorio-current.log
(6.8 KiB) Downloaded 11 times
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.

Loewchen
Global Moderator
Global Moderator
Posts: 8601
Joined: Wed Jan 07, 2015 5:53 pm
Contact:

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post 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".

Deadexow
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jun 14, 2024 1:47 pm
Contact:

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post 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.

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2767
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post 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.)
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics

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

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post 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
A good mod deserves a good changelog. Here's a tutorial (WIP) about Factorio's way too strict changelog syntax!

FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2767
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post 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. :P 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
Screenshot 2024-06-20 193025.png (282.3 KiB) Viewed 361 times
Edit: Could also use serpent on 'v':
Screenshot 2024-06-20 193512.png
Screenshot 2024-06-20 193512.png (1.47 MiB) Viewed 356 times
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics

Deadexow
Manual Inserter
Manual Inserter
Posts: 4
Joined: Fri Jun 14, 2024 1:47 pm
Contact:

Re: [1.1.107]The API function get_script_inventories() is not returning any value.

Post by Deadexow »

Thank you very much,now I know what the function return:

Code: Select all

{
	["mod_name"] = {inv,inv,……},
	["mod_name"] = {inv,inv,……},
}

Post Reply

Return to “Modding help”