[Solved] Attempt to index global 'game' (a nil value)

Place to get help with not working mods / modding interface.
Post Reply
BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

[Solved] Attempt to index global 'game' (a nil value)

Post by BrokenScience »

I recently moved some code from control to a new file to reduce clutter. Unfortunately it no longer works because of an error I did not get before. The file is required in control.lua and is called through on_init and on_configuration_changed, both of which do have access to the game table (I checked). Could the movement from control.lua mean I no longer have access to the game table?

I included the new file (mind the debug prints I commented out) and the lines left in control are:

Code: Select all

require("code.ordering")

if not item_order then item_order = {} end
if not frame_name then frame_name = 0 end

script.on_init(build_orderer())

script.on_configuration_changed(build_orderer())
I do hope it's not a parenthesis problem, but it probably is. That would be embarrassing.
Attachments
ordering.lua
New file from control
(3.14 KiB) Downloaded 126 times
Last edited by BrokenScience on Wed Aug 02, 2017 12:58 am, edited 1 time in total.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.

Jap2.0
Smart Inserter
Smart Inserter
Posts: 2339
Joined: Tue Jun 20, 2017 12:02 am
Contact:

Re: Attempt to index global 'game' (a nil value)

Post by Jap2.0 »

Don't know anything about lua, but I've done a bit of programming, so I know what mismatched parenthesis are. The only things I found were:

Code: Select all

function build_orderer()--debuggery)

Code: Select all

function order(items, orderer)--, debuggery)

Code: Select all

if can_be_ordered(orderer, orderer[items[i]]) then--, debuggery) then
which both have extra parenthesis, but it looks like -- denotes comments, so they wouldn't be it (not saying that it doesn't irritate me...)
There are 10 types of people: those who get this joke and those who don't.

User avatar
Mooncat
Smart Inserter
Smart Inserter
Posts: 1190
Joined: Wed May 18, 2016 4:55 pm
Contact:

Re: Attempt to index global 'game' (a nil value)

Post by Mooncat »

Change

Code: Select all

script.on_init(build_orderer())

script.on_configuration_changed(build_orderer())
to

Code: Select all

script.on_init(build_orderer)

script.on_configuration_changed(build_orderer)
( Remove the pair of () )

Without (), you are registering the delegate of build_orderer as the event handler, which is the intended and usual way to do so.
But with (), you are executing build_orderer() and registering its result as the event handler. It leads to at least 2 problems:
1. build_orderer() is executed once control.lua is loaded, even before on_init is invoked. "game" is not ready and hence it is nil.
2. build_orderer() does not return anything. Nothing will be registered.

(I know this, because I have made the same mistake before :P )

BrokenScience
Inserter
Inserter
Posts: 26
Joined: Thu Jul 21, 2016 5:31 pm
Contact:

Re: Attempt to index global 'game' (a nil value)

Post by BrokenScience »

Yep, it was the parentheses I included as part of the build_order() function.

That is a small detail that creates an incredibly complex problem.

Post Reply

Return to “Modding help”