LuaTransportLine always returning empty table

Place to get help with not working mods / modding interface.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

LuaTransportLine always returning empty table

Post by Reika »

Even with fully loaded belts, the result of get_contents() is always "{}", even when things like "get_item_count()" return nonzero values. What am I doing wrong?

EDIT: This cannot be right.

Code: Select all

game.print("Count of " .. line.get_item_count() .. " with contents table size " .. #line.get_contents())
Image
Image
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: LuaTransportLine always returning empty table

Post by eradicator »

You're doing it wrong™. get_contents() returns a dictionary (string indexed), not an array (continuous integer indexed) so # operations return undefined results. To iterate through the results you need to use a for item_name,count in pair(get_contents()) do loop.

Code: Select all

/c game.print(serpent.line(game.player.selected.get_transport_line(1).get_contents()))
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: LuaTransportLine always returning empty table

Post by Reika »

eradicator wrote:You're doing it wrong™. get_contents() returns a dictionary (string indexed), not an array (continuous integer indexed) so # operations return undefined results. To iterate through the results you need to use a for item_name,count in pair(get_contents()) do loop.

Code: Select all

/c game.print(serpent.line(game.player.selected.get_transport_line(1).get_contents()))
A k-v for loop was the first thing I tried (my code was literally doing that when I noticed it was not working). Nothing happened, because it was iterating over an empty set.
Image
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: LuaTransportLine always returning empty table

Post by eradicator »

Reika wrote:
eradicator wrote:You're doing it wrong™. get_contents() returns a dictionary (string indexed), not an array (continuous integer indexed) so # operations return undefined results. To iterate through the results you need to use a for item_name,count in pair(get_contents()) do loop.

Code: Select all

/c game.print(serpent.line(game.player.selected.get_transport_line(1).get_contents()))
A k-v for loop was the first thing I tried (my code was literally doing that when I noticed it was not working). Nothing happened, because it was iterating over an empty set.
So does the code i posted not work (i.e. print a table with belt content) when you use it in game (while selecting a belt with items on it)? I'm still on 16.12 (slow connection >_>), so there's a small possibility that it broke after that.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: LuaTransportLine always returning empty table

Post by Reika »

eradicator wrote:
Reika wrote:
eradicator wrote:You're doing it wrong™. get_contents() returns a dictionary (string indexed), not an array (continuous integer indexed) so # operations return undefined results. To iterate through the results you need to use a for item_name,count in pair(get_contents()) do loop.

Code: Select all

/c game.print(serpent.line(game.player.selected.get_transport_line(1).get_contents()))
A k-v for loop was the first thing I tried (my code was literally doing that when I noticed it was not working). Nothing happened, because it was iterating over an empty set.
So does the code i posted not work (i.e. print a table with belt content) when you use it in game (while selecting a belt with items on it)? I'm still on 16.12 (slow connection >_>), so there's a small possibility that it broke after that.
Not on that belt, no.

However, I also noticed that this issue is apparently ONLY for this one belt. I tried the command on another random belt and it worked fine. Do circuit connections and/or loaders affect the belt's LuaTransportLine?
Image
User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5211
Joined: Tue Jul 12, 2016 9:03 am
Contact:

Re: LuaTransportLine always returning empty table

Post by eradicator »

Works fine here on a random circuit connected (en-/disabled) belt. I can see a loader on your picture. Maybe that's causing some issues. Can't test loaders myself. But splitters also always return {}.
Author of: Belt Planner, Hand Crank Generator, Screenshot Maker, /sudo and more.
Mod support languages: 日本語, Deutsch, English
My code in the post above is dedicated to the public domain under CC0.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: LuaTransportLine always returning empty table

Post by Reika »

eradicator wrote:Works fine here on a random circuit connected (en-/disabled) belt. I can see a loader on your picture. Maybe that's causing some issues. Can't test loaders myself. But splitters also always return {}.
Odd. Loaders are a prime suspect to me as well, given that these ones use custom logic.
Image
Rseding91
Factorio Staff
Factorio Staff
Posts: 16014
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: LuaTransportLine always returning empty table

Post by Rseding91 »

It looks like that transport belt doesn't have items on one side? Try (2) to see if the other line is the one you're looking for.
If you want to get ahold of me I'm almost always on Discord.
User avatar
Reika
Filter Inserter
Filter Inserter
Posts: 587
Joined: Tue May 19, 2015 1:56 am
Contact:

Re: LuaTransportLine always returning empty table

Post by Reika »

Rseding91 wrote:It looks like that transport belt doesn't have items on one side? Try (2) to see if the other line is the one you're looking for.
It was not in the image, but I was trying both.
Image
Post Reply

Return to “Modding help”