Page 1 of 1

What am I missing? Lua API

Posted: Sat Mar 30, 2024 7:01 pm
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

Re: What am I missing? Lua API

Posted: Sat Mar 30, 2024 7:48 pm
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.

Re: What am I missing? Lua API

Posted: Sat Mar 30, 2024 8:24 pm
by Philipp
I get the same error when I use on_init

Re: What am I missing? Lua API

Posted: Sat Mar 30, 2024 8:55 pm
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.

Re: What am I missing? Lua API

Posted: Sat Mar 30, 2024 9:05 pm
by Philipp
Thanks, that fixed it!