Page 1 of 1
How to access prototypes during script load?
Posted: Sun Nov 13, 2016 5:35 am
by Reika
I have a mod whose primary purpose is to revamp ore and enemy distribution, but I am adding an option to add a fourth tier of worm (green 'behemoth', like the biters and spitters) for the generation.
I have a 'constants' class, which, among other things, '
statically' defines the spawnable worm types as an array:
Code: Select all
worm_sizes = {"small-worm-turret", "medium-worm-turret", "big-worm-turret"}
Which is used in the spawning code:
Code: Select all
function getWormType(dist)
local s = getCosInterpolate(dist-min_worm_dist, full_spawn_dist, #worm_sizes)
s = math.min(s, #worm_sizes-0.25)
s = s+getRandPM(0.5) --randomize worm spawns a bit
s = math.floor(s+0.5) --round
if s > 0 and s <= #worm_sizes then
return worm_sizes[s]
else
return nil
end
end
I want to add the fourth tier of worm, but only if the prototype exists, either from me, or - and this is important, because it nullifies the "just check your config" answer - any other mod. I thought this would be a trivial check in the initialization:
Code: Select all
if game.entity_prototypes.turret["behemoth-worm-turret"] then
table.insert(worm_sizes, "behemoth-worm-turret")
end
However, that gives me a 'game is nil' error on loading a world. Data.raw is also inaccessible at this time, so that leads to my question: If this is before the LuaGameScript is available, but too late to access the raw data table, how can I check the existence of an entity type? I really do NOT want to resort to a hacky mess like populating the array on the first game tick or something.
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 11:53 am
by Rseding91
Prototypes can't be changed after the game is started - you have to define all prototypes in the data loading stage.
See:
http://lua-api.factorio.com/latest/Data-Lifecycle.html
If your goal is to create them runtime on_chunk_generated then you can use the on_init event when a game is started to detect/setup anything you like:
http://lua-api.factorio.com/latest/LuaB ... ap.on_init
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 7:00 pm
by Reika
I am not trying to edit or create prototypes, only query their existence.
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 8:14 pm
by Rseding91
Reika wrote:
I am not trying to edit or create prototypes, only query their existence.
Then on_init should work great for what you're after

Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 10:13 pm
by Reika
Rseding91 wrote:Reika wrote:
I am not trying to edit or create prototypes, only query their existence.
Then on_init should work great for what you're after

According to the documentation:
This is called once when a new save game is created or once when a save file is loaded that previously didn't contain the mod. This is always called before other event handlers and is meant for setting up initial values that a mod will use for its lifetime.
I need something that will run every time the save is loaded. For one, my existing save (which has always had this mod and I want this to take effect for) requires it, and secondly, this array is not saved anywhere; it is generated on launch. As a result, using on_init would only add green worms the first time it is loaded.
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 10:27 pm
by prg
So put that variable into global and handle mod version changes in on_configuration_changed.
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 10:43 pm
by Reika
prg wrote:So put that variable into global and handle mod version changes in on_configuration_changed.
One, global cannot be properly used in on_init:
Note, although the global table has not been setup if a mod does populate the table with some data it will be overwritten by any loaded data.
Two, I have never gotten global saving working; it always gives me some sort of illegal access error (not 'nil value', literally something to the effect of 'you are not allowed to access this'), even when trying to access it from on_tick and similar.
Re: How to access prototypes during script load?
Posted: Sun Nov 13, 2016 11:03 pm
by prg
Reika wrote:One, global cannot be properly used in on_init:
Note, although the global table has not been setup if a mod does populate the table with some data it will be overwritten by any loaded data.
Where is that from? Putting stuff into global in on_init works for me.
Reika wrote:Two, I have never gotten global saving working; it always gives me some sort of illegal access error (not 'nil value', literally something to the effect of 'you are not allowed to access this'), even when trying to access it from on_tick and similar.
global must not be modified in on_load, but keeping stuff around over saves is the whole point of it. Can you provide some code that shows this problem?
Re: How to access prototypes during script load?
Posted: Mon Nov 14, 2016 12:44 am
by Reika
prg wrote:Reika wrote:One, global cannot be properly used in on_init:
Note, although the global table has not been setup if a mod does populate the table with some data it will be overwritten by any loaded data.
Where is that from? Putting stuff into global in on_init works for me.
Linked earlier in the thread:
http://lua-api.factorio.com/latest/Data-Lifecycle.html
Re: How to access prototypes during script load?
Posted: Mon Nov 14, 2016 7:18 am
by prg
Reika wrote:prg wrote:Reika wrote:One, global cannot be properly used in on_init:
Note, although the global table has not been setup if a mod does populate the table with some data it will be overwritten by any loaded data.
Where is that from? Putting stuff into global in on_init works for me.
Linked earlier in the thread:
http://lua-api.factorio.com/latest/Data-Lifecycle.html
So that refers to "3. control.lua initialization" for things that happen outside of any event handlers like on_init. Still sounds like on_init/on_configuration_changed would be the place to put your stuff.
Re: How to access prototypes during script load?
Posted: Mon Nov 14, 2016 10:17 pm
by Reika
prg wrote:Reika wrote:prg wrote:Reika wrote:One, global cannot be properly used in on_init:
Note, although the global table has not been setup if a mod does populate the table with some data it will be overwritten by any loaded data.
Where is that from? Putting stuff into global in on_init works for me.
Linked earlier in the thread:
http://lua-api.factorio.com/latest/Data-Lifecycle.html
So that refers to "3. control.lua initialization" for things that happen outside of any event handlers like on_init. Still sounds like on_init/on_configuration_changed would be the place to put your stuff.
I will try that, but one question: If one were to add the mod to an existing world, would both on_init and on_changed fire, causing me to add the green worms twice?
Re: How to access prototypes during script load?
Posted: Mon Nov 14, 2016 10:44 pm
by Rseding91
Reika wrote:I will try that, but one question: If one were to add the mod to an existing world, would both on_init and on_changed fire, causing me to add the green worms twice?
Yes, you'd obviously want to check that the item wasn't in your list already as on_configuration_changed can end up being called many times as users change mods or update the game.