Page 1 of 1
Apparently 'game' and 'script' are nil.
Posted: Wed May 18, 2016 3:23 am
by bluMyst
What's going on? This is exasperating.
Code: Select all
-- control.lua
print(game.local_player)
Code: Select all
__factorio-mod-template__/data.lua:1: __factorio-mod-template__/control.lua:1: attempt to index global 'game' (a nil value)
Re: Apparently 'game' and 'script' are nil.
Posted: Wed May 18, 2016 5:55 am
by Arch666Angel
You are running the control.lua in the data phase, when there is no game/player. Just delete the require 'control' from data.lua, control.lua is loaded automatically when a game loads/initializes.
Re: Apparently 'game' and 'script' are nil.
Posted: Wed May 18, 2016 6:01 am
by bluMyst
Arch666Angel wrote:You are running the control.lua in the data phase, when there is no game/player. Just delete the require 'control' from data.lua, control.lua is loaded automatically when a game loads/initializes.
Interesting! Thanks.

So does any .lua file in the top-level directory of a mod folder get automatically run like that? Or is it just control.lua? And also, what's special about data.lua that causes it to be run at a different time? Is that just what happens to any file called data.lua?
Re: Apparently 'game' and 'script' are nil.
Posted: Wed May 18, 2016 7:23 am
by ArderBlackard
There is a page on Factorio Wiki describing the purpose of these special files:
https://wiki.factorio.com/index.php?tit ... _Lifecycle
TL;DR:
1. On Factorio startup 'data.lua' scripts are executed for all mods in their registration order, then 'data.updates.lua', then 'data-final-fixes.lua' (these iterations allow mods which are registered earlier still have chance to override something from later mods). These files just define prototypes and obviously don't have 'game' or 'script' instances.
2. On every game start or load the 'control.lua' scripts of every mod is executed. They are used for defining the game logic and don't have an access to the 'data' table from 'data*.lua' files