What am I missing? Lua API

Place to get help with not working mods / modding interface.
Philipp
Burner Inserter
Burner Inserter
Posts: 9
Joined: Mon Aug 21, 2023 9:18 am
Contact:

What am I missing? Lua API

Post by Philipp »

Hello together,

I´m realyy struggling to get a simple script from the API Docs running.
Just can´t figure out what I´m missing. I guess its pretty simple...

The code:

Code: Select all

--control.lua

script.on_load(function()
    local surface = game.surfaces["nauvis"]
    surface.create_entity({
		name = "big-biter", 
		position = {15, 3}, 
		force = game.forces.player
    })
end)
The game tells me:
Image

Thanks for your help! :D
robot256
Filter Inserter
Filter Inserter
Posts: 994
Joined: Sun Mar 17, 2019 1:52 am
Contact:

Re: What am I missing? Lua API

Post by robot256 »

It doesn't match the error given, but the code you ran won't work anyways. On_load is only allowed to read variables, not alter the game state by creating entities. On_load runs when you load a savegame, and the simulation is supposed to be unaware of whether it has been saved and loaded or just kept running.

You can use on_init for code that runs the first time a save is created, or on_nth_tick for something that happens at set time intervals.
Philipp
Burner Inserter
Burner Inserter
Posts: 9
Joined: Mon Aug 21, 2023 9:18 am
Contact:

Re: What am I missing? Lua API

Post by Philipp »

I get the same error when I use on_init
User avatar
boskid
Factorio Staff
Factorio Staff
Posts: 3272
Joined: Thu Dec 14, 2017 6:56 pm
Contact:

Re: What am I missing? Lua API

Post by boskid »

Your error suggests that you included control.lua from data.lua. Data stage modding (that goes through data.lua) is supposed to create all the prototypes that will be available later, it runs way before save file is even loaded, even before main menu shows up. control.lua is from runtime stage which happens after all prototypes were already created and when a save is created or loaded.

Do not include control.lua from data.lua.
Philipp
Burner Inserter
Burner Inserter
Posts: 9
Joined: Mon Aug 21, 2023 9:18 am
Contact:

Re: What am I missing? Lua API

Post by Philipp »

Thanks, that fixed it!
Post Reply

Return to “Modding help”