Page 1 of 1

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

Posted: Thu Jul 06, 2017 12:49 pm
by BrokenScience
The game does not allow me to use script.on_event or any other of the functions stored in script. It works fine with all my other mods but, in this particular one, crashes and brings up the error: "attempt to index global 'script' (a nil value)". What is causing this and how do I fix it?

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

Posted: Thu Jul 06, 2017 12:53 pm
by Nexela
script is only valid from control.lua (and files you require from control.lua)

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

Posted: Thu Jul 06, 2017 3:09 pm
by darkfrei
Can you show your code?

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

Posted: Fri Jul 07, 2017 8:00 am
by bobingabout
I can't help you figure out what is wrong, since you haven't shown us anything.
Literally all you've told us is "A script in my mod doesn't work", okay, that's nice, more info please?

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

Posted: Fri Jul 07, 2017 5:30 pm
by BrokenScience

Code: Select all

script.on_event("Eco", function(event)
	for each in data do
		if each[type] == "recipe" then
			table.insert(recipes, each)
		end
	end
	local test = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = ui.add({type = "frame", name = "test_frame", direction = "vertical"})
	for each in recipes do
		local label = frame.add({type = "label", name = each[name], caption = each[name]})
	end
end)
This is the code. Ignore the contents of the function, I know it is wrong. I am trying to test something.

"Eco" is a custom key. All of this is in control.lua for the mod. If I put this script.on_event into on_mod_load or on_mod_init function it no longer has the error but is completely useless.

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

Posted: Fri Jul 07, 2017 6:01 pm
by Nexela
data is only availble in the data stage, script is only available in the control stage, you are trying to mix the 2

http://lua-api.factorio.com/latest/Data-Lifecycle.html

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

Posted: Wed Jul 12, 2017 8:57 pm
by BrokenScience
That was it. How do I access the recipes and items then in this stage if I cannot use data?

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

Posted: Wed Jul 12, 2017 9:04 pm
by prg

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

Posted: Fri Jul 21, 2017 1:07 am
by BrokenScience
I replaced data now with game.recipe_prototypes so now I have this:

Code: Select all

script.on_event("Eco", function(event)
	local test = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = ui.add({type = "frame", name = "test_frame", direction = "vertical"})
	for each in game.recipe_prototypes do
		local label = frame.add({type = "label", name = each[name], caption = each[name]})
	end
end)
I now have the same error again (Attempt to index global 'script' (a nil value)). What is wrong with this to cause script.on_event to not register?

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

Posted: Fri Jul 21, 2017 7:49 am
by Mooncat
It will be more helpful if you can provide us the whole lua file instead of just the snippet.

The error message is a bit strange. If the code is really in control.lua, "script" shouldn't be nil. But the error says it is...

Right now, I can only tell that you are doing wrong with the event system. If you want to use custom event, you will need to register it first using script.generate_event_name() and use the returned value to replace "Eco".
Also, when you raise the event, you should use the same returned value of script.generate_event_name(). Because if you read the document carefully, script.raise_event actually accepts the unsigned-integral ID of of the event, not the event name (string).

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

Posted: Sat Jul 22, 2017 12:53 am
by BrokenScience
Mooncat wrote:It will be more helpful if you can provide us the whole lua file instead of just the snippet.
The code above is the entire Lua file of control.Lua with the exception of a single comment above it that says, "-- test", but I added it anyway.

What is the difference between:

Code: Select all

data:extend({
	{
    type = "custom-input",
    name = "Eco",
    key_sequence = "SHIFT + R",
    consuming = "script-only"
	}
})
In data.Lua and what I have above in control.lua and what you suggested? I have another mod that uses the same method for events and works fine (ItemGiverGui). I have been using this as an example for Gui and events but i'm getting the feeling that it may not be the best example. (I attached the control.lua of the mod I have as I added comments that made it easier for me to scan and might help.)

Also I am confused as to where the linking of the custom input and code to the event happens with your method. Is this in addition to what I have right now? Does the custom input and code get passed to script.raise_event(event, table) in the table somewhere?

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

Posted: Sat Jul 22, 2017 4:07 am
by Nexela
data is where prototypes go, control is where game scripts go

control.lua

Code: Select all

script.on_event("Eco", function(event)
    local player = game.players[event.player_index]
	local main_frame = player.gui.center.add({type = "frame", name = "test", direction = "vertical"})
	local frame = main_frame.add({type = "frame", name = "test_frame", direction = "vertical"})
	for recipe_name in pairs(game.recipe_prototypes) do
		frame.add({type = "label", name = recipe_name, caption = recipe_name})
	end
end)

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

Posted: Sun Jul 23, 2017 1:09 am
by BrokenScience
What you have given me there crashes with the same error when in control.lua. The problem I'm having is with script in the script.on_event, not the contents (although I know it does not work). I went and deleted the entirety of the contents of the function so I had only:

Code: Select all

script.on_event("Eco", function(event)
end)
This also gives me the error. It is like script has been wiped from the game.

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

Posted: Sun Jul 23, 2017 2:07 am
by Nexela
Can you post the whole mod zipped up?

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

Posted: Sun Jul 23, 2017 7:29 am
by prg
Can't reproduce.

data.lua:

Code: Select all

data:extend({
   {
    type = "custom-input",
    name = "Eco",
    key_sequence = "SHIFT + R",
    consuming = "script-only"
   }
})
control.lua:

Code: Select all

script.on_event("Eco", function(event)
    game.players[event.player_index].print("foo")
end)
Hit shift+r -> prints "foo".

You aren't requiring control.lua from data.lua or something like that, right?

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

Posted: Mon Jul 24, 2017 11:07 pm
by BrokenScience
Yes I am. No idea how that got there. (Solved)