Page 1 of 1
[Solved] Why would util be nil?
Posted: Tue Aug 09, 2016 3:19 pm
by Versepelles
I got a bug report for a mod of mine where the user claimed that they were on the latest experimental version of Factorio and got this message:
Code: Select all
VersepellesDeepQuarry/prototypes/entity.lua:4: attempt to index field 'table' (a nil value)
This is the beginning of the appropriate file:
Code: Select all
local ent -- placeholder for building each entity
-- Deep Quarry
ent = util.table.deepcopy(data.raw["container"]["steel-chest"])
So, it looks to me like
util is
nil for that user (I am not able to reproduce the error). What could be causing this?
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 3:28 pm
by steinio
require "util" ?
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 3:33 pm
by Versepelles
steinio wrote:require "util" ?
I thought it was loaded automatically? Why would their system need it to be required while my system would not? I'm not familiar with the intricacies of load order.
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 3:40 pm
by Zeblote
The "data.lua" files for all mods are loaded in the same interpreter, so maybe you have another mod that loaded it but he doesn't?
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 3:45 pm
by Versepelles
Zeblote wrote:The "data.lua" files for all mods are loaded in the same interpreter, so maybe you have another mod that loaded it but he doesn't?
I usually test my mods in a clean environment (no others), so this is confusing. Guess I have to go update them all. Still not sure where util.lua is being loaded from, but that does answer my question. Thanks.
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 3:52 pm
by daniel34
Versepelles wrote:Zeblote wrote:The "data.lua" files for all mods are loaded in the same interpreter, so maybe you have another mod that loaded it but he doesn't?
I usually test my mods in a clean environment (no others), so this is confusing. Guess I have to go update them all. Still not sure where util.lua is being loaded from, but that does answer my question. Thanks.
The base mod also counts as mod in that regard and is always loaded before all other mods. It requires util in the first line of data\base\data.lua.
The only way I can think of for util or util.table to be nil would be if he modified the base or core files or uses a mod that redefines util or util.table (not sure if that would work in lua).
Re: [Solved] Why would util be nil?
Posted: Tue Aug 09, 2016 4:06 pm
by steinio
Maybe base is not mentioned in the depency of this mod?
This sets the load order.
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 4:08 pm
by Versepelles
daniel34 wrote:Versepelles wrote:Zeblote wrote:The "data.lua" files for all mods are loaded in the same interpreter, so maybe you have another mod that loaded it but he doesn't?
I usually test my mods in a clean environment (no others), so this is confusing. Guess I have to go update them all. Still not sure where util.lua is being loaded from, but that does answer my question. Thanks.
The base mod also counts as mod in that regard and is always loaded before all other mods. It requires util in the first line of data\base\data.lua.
The only way I can think of for util or util.table to be nil would be if he modified the base or core files or uses a mod that redefines util or util.table (not sure if that would work in lua).
So I shouldn't normally need to require util in Factorio, or is it good practice to do so?
Re: [Solved] Why would util be nil?
Posted: Tue Aug 09, 2016 4:35 pm
by orzelek
Hmm I'd recommend trying to use table.deepcopy directly.
It works in default name space without problems.
Re: [Solved] Why would util be nil?
Posted: Tue Aug 09, 2016 4:36 pm
by Versepelles
steinio wrote:Maybe base is not mentioned in the depency of this mod?
This sets the load order.
Code: Select all
"dependencies": ["base >= 0.13.0"]
That's from the info.json, so it should be loaded. Attached is the rest of the mod, if you are interested.
orzelek wrote:Hmm I'd recommend trying to use table.deepcopy directly.
It works in default name space without problems.
I'll do that from now on.
Re: Why would util be nil?
Posted: Tue Aug 09, 2016 5:05 pm
by daniel34
Versepelles wrote:So I shouldn't normally need to require util in Factorio, or is it good practice to do so?
You shouldn't need to require it as it is already loaded by the base mod, but if there is another mod loaded before yours and it removes/modifies it (by overwriting table.deepcopy for example) you will be calling the new function instead of the util.lua one.
A simple call of
table.deepcopy = nil (or other stupid stuff)* in another mod would break your mod, so I recommend you do require util in your mod so that can't happen.
* I actually did test this and broke your mod.
Re: [Solved] Why would util be nil?
Posted: Wed Aug 10, 2016 1:02 am
by aubergine18
Would it not be easier to set a dependency for the base mod?
Re: [Solved] Why would util be nil?
Posted: Mon Aug 22, 2016 7:19 pm
by doktorstick
Surfaces is one such mode that wipes out
util.
I received an error report for example the same reason. Luckily the bug reporter gave me a zip of the mod directory and after a binary search of the mods, I found that my mod fails with Surfaces due to:
Code: Select all
$ head -n 20 script/lib/util.lua
--[[
Surfaces (Factorio Mod)
Copyright (C) 2016 Simon Crawley
This work is licensed under the Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License. To view a copy of this license, visit http://creativecommons.org/licenses/by-nc-sa/4.0/ or send a letter to Creative Commons, PO Box 1866, Mountain View, CA 94042, USA.
]]
require("script.lib.api")
require("script.lib.util-base")
--[[--
Miscellaneous utilities
@module util
]]
util = {}
And as expected, commenting out the
util = {} allows my module to load. Likewise, adding
require "util" to my mod fixed the problem.
I guess, too, that
require "defines" should be explicitly added in case someone does their own module with the same name.