[Solved] Why would util be nil?

Place to get help with not working mods / modding interface.
User avatar
Versepelles
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sat May 28, 2016 1:42 pm
Contact:

[Solved] Why would util be nil?

Post 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?
Last edited by Versepelles on Tue Aug 09, 2016 3:46 pm, edited 1 time in total.
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: Why would util be nil?

Post by steinio »

require "util" ?
Image

Transport Belt Repair Man

View unread Posts
User avatar
Versepelles
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sat May 28, 2016 1:42 pm
Contact:

Re: Why would util be nil?

Post 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.
Zeblote
Filter Inserter
Filter Inserter
Posts: 973
Joined: Fri Oct 31, 2014 11:55 am
Contact:

Re: Why would util be nil?

Post 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?
User avatar
Versepelles
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sat May 28, 2016 1:42 pm
Contact:

Re: Why would util be nil?

Post 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.
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Why would util be nil?

Post 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).
quick links: log file | graphical issues | wiki
User avatar
steinio
Smart Inserter
Smart Inserter
Posts: 2638
Joined: Sat Mar 12, 2016 4:19 pm
Contact:

Re: [Solved] Why would util be nil?

Post by steinio »

Maybe base is not mentioned in the depency of this mod?
This sets the load order.
Image

Transport Belt Repair Man

View unread Posts
User avatar
Versepelles
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sat May 28, 2016 1:42 pm
Contact:

Re: Why would util be nil?

Post 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?
orzelek
Smart Inserter
Smart Inserter
Posts: 3922
Joined: Fri Apr 03, 2015 10:20 am
Contact:

Re: [Solved] Why would util be nil?

Post by orzelek »

Hmm I'd recommend trying to use table.deepcopy directly.
It works in default name space without problems.
User avatar
Versepelles
Long Handed Inserter
Long Handed Inserter
Posts: 70
Joined: Sat May 28, 2016 1:42 pm
Contact:

Re: [Solved] Why would util be nil?

Post 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.
Attachments
VersepellesDeepQuarry_1.2.1.zip
(88.14 KiB) Downloaded 68 times
daniel34
Global Moderator
Global Moderator
Posts: 2761
Joined: Thu Dec 25, 2014 7:30 am
Contact:

Re: Why would util be nil?

Post 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.
quick links: log file | graphical issues | wiki
User avatar
aubergine18
Smart Inserter
Smart Inserter
Posts: 1264
Joined: Fri Jul 22, 2016 8:51 pm
Contact:

Re: [Solved] Why would util be nil?

Post by aubergine18 »

Would it not be easier to set a dependency for the base mod?
Better forum search for modders: Enclose your search term in quotes, eg. "font_color" or "custom-input" - it prevents the forum search from splitting on hypens and underscores, resulting in much more accurate results.
doktorstick
Fast Inserter
Fast Inserter
Posts: 219
Joined: Fri Aug 12, 2016 10:22 pm
Contact:

Re: [Solved] Why would util be nil?

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

Return to “Modding help”