Apparently 'game' and 'script' are nil.

Place to get help with not working mods / modding interface.
Post Reply
bluMyst
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun May 15, 2016 3:53 am
Contact:

Apparently 'game' and 'script' are nil.

Post by bluMyst »

What's going on? This is exasperating.

Code: Select all

-- data.lua
require 'control'

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)
In my infinite ambivalence.

User avatar
Arch666Angel
Smart Inserter
Smart Inserter
Posts: 1636
Joined: Sun Oct 18, 2015 11:52 am
Contact:

Re: Apparently 'game' and 'script' are nil.

Post 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.

bluMyst
Burner Inserter
Burner Inserter
Posts: 10
Joined: Sun May 15, 2016 3:53 am
Contact:

Re: Apparently 'game' and 'script' are nil.

Post 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?
In my infinite ambivalence.

User avatar
ArderBlackard
Long Handed Inserter
Long Handed Inserter
Posts: 74
Joined: Thu May 05, 2016 12:41 pm
Contact:

Re: Apparently 'game' and 'script' are nil.

Post 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
Gib dich hin bis du Glück bist

Post Reply

Return to “Modding help”