Page 1 of 1

serpent.block(data.raw) incorrect values

Posted: Thu Nov 05, 2020 7:47 pm
by moon69
I finally figured out why so many fields in my

Code: Select all

log(serpent.block(data.raw))
were 0 where I expected tables...
  • serpent.block() outputs nil for shared/self-referenced tables,
  • Factorio's version of serpent changes nil to 0 (patch 0.18.15)
  • Factorio's version of serpent doesn't allow comments turns comments off by default, so you don't get the --[[ref]] warning on the line in question or the --[[incomplete output with shared/self-references skipped]] comment at the end of the block
NB: This isn't an issue if you log the required object directly:

Code: Select all

log(serpent.block(data.raw.item.["bob-logistic-robot-5"]))
A clumsy workaround is using serpent.dump() instead:

Code: Select all

...
 ["bob-logistic-robot-5"] = {
  icon_size = 32,
  icons = {
   {icon = "__reskins-bobs__/graphics/icons/logistics/logistic-robot/logistic-robot-icon-base.png", icon_mipmaps = 4, icon_size = 64} --[[table]],
   {
    icon = "__reskins-bobs__/graphics/icons/logistics/logistic-robot/logistic-robot-icon-mask.png",
    icon_mipmaps = 4,
    icon_size = 64,
    tint = 0
   },
   ...
then after the whole table is dumped will show the shared references like this...

Code: Select all

_["bob-logistic-robot-5"].icons[2].tint = _["assembling-machine-6"].icons[2].tint

Re: serpent.block(data.raw) incorrect values

Posted: Thu Nov 05, 2020 9:01 pm
by boskid
I could just restore old behavior with nil's if highly requested because script context is no longer saved using serpent (0.18.28).

Re: serpent.block(data.raw) incorrect values

Posted: Thu Nov 05, 2020 11:23 pm
by eradicator
moon69 wrote:
Thu Nov 05, 2020 7:47 pm
  • Factorio's version of serpent doesn't allow comments, so you don't get the --[[ref]] warning on the line in question or the --[[incomplete output with shared/self-references skipped]] comment at the end of the block
The link correctly says "by default". Nothing stops you from turning comments on again. It's not always desync safe, but that doesn't matter if you just want to look at data.raw.

Re: serpent.block(data.raw) incorrect values

Posted: Fri Nov 06, 2020 12:41 am
by moon69
boskid wrote:
Thu Nov 05, 2020 9:01 pm
I could just restore old behavior with nil's if highly requested because script context is no longer saved using serpent (0.18.28).
Thanks, but don't think nil vs 0 would make much difference - it was really just obfuscating the underlying working of serpent.

I've hacked serpent.lua and it seems to work OK...
if seen[t] then -- already seen this element
sref[#sref+1] = spath..space..'='..space..seen[t]
return tag..'nil'..comment('ref', level) end

I'm not sure if this will affect circular references etc. or not though.

Re: serpent.block(data.raw) incorrect values

Posted: Fri Nov 06, 2020 12:49 am
by moon69
eradicator wrote:
Thu Nov 05, 2020 11:23 pm
The link correctly says "by default". Nothing stops you from turning comments on again. It's not always desync safe, but that doesn't matter if you just want to look at data.raw.
Thanks - thought I tested that - but obviously not! Certainly helps in identifying which values are affected.
Still need to do serpent.dump() or other shenanigans to see where it's referencing though.

Wube seems to have customised the --[[ref]] comments to include the name of the source table :)

Code: Select all

tint = 0 --[=[ ref [""]["assembling-machine-6"].icons[2].tint ]=]

Re: serpent.block(data.raw) incorrect values

Posted: Fri Nov 06, 2020 1:34 am
by eradicator
moon69 wrote:
Fri Nov 06, 2020 12:49 am
Wube seems to have customised the --[[ref]] comments to include the name of the source table :)

Code: Select all

tint = 0 --[=[ ref [""]["assembling-machine-6"].icons[2].tint ]=]
Interesting. I never noticed and built that in myself: here.