Page 1 of 1

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

Posted: Tue Aug 01, 2017 2:16 am
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.

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

Posted: Tue Aug 01, 2017 4:07 am
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...)

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

Posted: Tue Aug 01, 2017 7:54 am
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 )

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

Posted: Wed Aug 02, 2017 12:58 am
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.