Page 1 of 1

LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 3:40 am
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

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 5:34 am
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()))

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 6:28 am
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.

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 7:52 am
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.

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 8:09 am
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?

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 8:19 am
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 {}.

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 8:51 am
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.

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 9:17 am
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.

Re: LuaTransportLine always returning empty table

Posted: Sat Jan 13, 2018 9:45 am
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.