serpent.block(data.raw) incorrect values

Place to get help with not working mods / modding interface.
Post Reply
User avatar
moon69
Fast Inserter
Fast Inserter
Posts: 181
Joined: Sun Sep 18, 2016 6:53 pm
Contact:

serpent.block(data.raw) incorrect values

Post 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
Last edited by moon69 on Fri Nov 06, 2020 12:54 am, edited 1 time in total.

User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 2250
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

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

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

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

Post 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.
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
moon69
Fast Inserter
Fast Inserter
Posts: 181
Joined: Sun Sep 18, 2016 6:53 pm
Contact:

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

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

User avatar
moon69
Fast Inserter
Fast Inserter
Posts: 181
Joined: Sun Sep 18, 2016 6:53 pm
Contact:

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

Post 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 ]=]

User avatar
eradicator
Smart Inserter
Smart Inserter
Posts: 5206
Joined: Tue Jul 12, 2016 9:03 am
Contact:

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

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

Post Reply

Return to “Modding help”