[Solved] Attempt to index global 'script' (a nil value)
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
[Solved] Attempt to index global 'script' (a nil value)
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?
Last edited by BrokenScience on Tue Aug 01, 2017 1:46 am, edited 2 times in total.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
script is only valid from control.lua (and files you require from control.lua)
Re: Attempt to index global 'script' (a nil value)
Can you show your code?
- bobingabout
- Smart Inserter
- Posts: 7352
- Joined: Fri May 09, 2014 1:01 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
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?
Literally all you've told us is "A script in my mod doesn't work", okay, that's nice, more info please?
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
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)
"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.
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
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
http://lua-api.factorio.com/latest/Data-Lifecycle.html
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
That was it. How do I access the recipes and items then in this stage if I cannot use data?
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
I replaced data now with game.recipe_prototypes so now I have this:
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?
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)
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
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).
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).
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
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.Mooncat wrote:It will be more helpful if you can provide us the whole lua file instead of just the snippet.
What is the difference between:
Code: Select all
data:extend({
{
type = "custom-input",
name = "Eco",
key_sequence = "SHIFT + R",
consuming = "script-only"
}
})
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?
- Attachments
-
- control.lua
- ItemGiverGui control.lua with comments I added
- (13.63 KiB) Downloaded 236 times
-
- control.lua
- My control.lua
- (367 Bytes) Downloaded 237 times
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
data is where prototypes go, control is where game scripts go
control.lua
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)
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
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:
This also gives me the error. It is like script has been wiped from the game.
Code: Select all
script.on_event("Eco", function(event)
end)
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.
Re: Attempt to index global 'script' (a nil value)
Can you post the whole mod zipped up?
Re: Attempt to index global 'script' (a nil value)
Can't reproduce.
data.lua:
control.lua:
Hit shift+r -> prints "foo".
You aren't requiring control.lua from data.lua or something like that, right?
data.lua:
Code: Select all
data:extend({
{
type = "custom-input",
name = "Eco",
key_sequence = "SHIFT + R",
consuming = "script-only"
}
})
Code: Select all
script.on_event("Eco", function(event)
game.players[event.player_index].print("foo")
end)
You aren't requiring control.lua from data.lua or something like that, right?
Automatic Belt (and pipe) Planner—Automate yet another aspect of constructing your factory!
-
- Inserter
- Posts: 26
- Joined: Thu Jul 21, 2016 5:31 pm
- Contact:
Re: Attempt to index global 'script' (a nil value)
Yes I am. No idea how that got there. (Solved)
Smashing a brick wall with my face would be a lot more rewarding if I didn't just reveal 3 more.