Page 1 of 1
A few Questions
Posted: Wed Jul 27, 2016 7:26 am
by kasandraen
Hi!
I have tried to create a mod that read of the all the ingame generators, and make a primary, secondary and terciary version of them. And then you can use certan keybind to upgrade/downgrade a generators output.
The issues I've gotten into is:
The program crashed when I try to replace the engine, with the error "Lua.entity doesnt contain the key destroy"
the part of the code relevant is:
Code: Select all
script.on_event("decrease-generator-priority", function(event)
local selection = game.players[event.player_index].selected
if selection and selection.type == "generator" then
DecreasePriority(selection, event, force)
end
end
function DecreasePriority(entity, event, force)
if string.find(Name, "-Primary") then
print("The generator is now secondary")
entity.destoy()
if game.prototypes["GP-".. Name .. "-Secondary"] then
surface.create_entity({name = "GP-".. Name .. "-Secondary", postition = Pos, direction = Rot, force = force})
else
print("Missing the generator")
end
end
What am I missing?
and 2.
Is there a way to generate the Locale on the go?
Like can I use
Code: Select all
local file = io.open(__GeneratorPriority__/locale/en/entity.cfg, "w")
and generate appropiate names into it?
Re: A few Questions
Posted: Wed Jul 27, 2016 8:03 am
by Choumiko
There is a typo in DecreasePriority: entity.destoy instead of dest
roy(), and you might do the destruction just before creating the new entity, so the player does not have to place it again in case the generator is missing
2: try using something like the following in your data.lua or data-updates, data-final-fixes where you create the prototypes
Code: Select all
for _, generator in pairs() do
generator.localized_name = {"generator_priority_terciary", "entity-name." .. generator.name}
end
Typos etc aside this should create something like "Steam engine (terciary)" if you have a local in your mod
Code: Select all
generator_priority_terciary = __1__ (terciary)
From
viewtopic.php?f=93&t=6847&start=290#p179290
Re: A few Questions
Posted: Wed Jul 27, 2016 8:28 am
by kasandraen
Choumiko wrote:There is a typo in DecreasePriority: entity.destoy instead of dest
roy(), and you might do the destruction just before creating the new entity, so the player does not have to place it again in case the generator is missing
2: try using something like the following in your data.lua or data-updates, data-final-fixes where you create the prototypes
Code: Select all
for _, generator in pairs() do
generator.localized_name = {"generator_priority_terciary", "entity-name." .. generator.name}
end
Typos etc aside this should create something like "Steam engine (terciary)" if you have a local in your mod
Code: Select all
generator_priority_terciary = __1__ (terciary)
From
viewtopic.php?f=93&t=6847&start=290#p179290
Oh my god... I sat here pondering for hours what I had done wrong with the destroy thing... missing a a r......
And ill try the locale thing, thanks!

Re: A few Questions
Posted: Wed Jul 27, 2016 8:39 am
by kasandraen
Well new issue.. It refuses to spawn the new unit, using the surface.create_entity returns "Attempt to index global "surface" a nil" and using what the wiki says, game.get_surface(1).create_entity returns "LuaGameScript doesnt contain key get_surface"... sigh.. whats the point of a wiki if it doesnt explain shit...
Re: A few Questions
Posted: Wed Jul 27, 2016 9:08 am
by Choumiko
The wiki tends to be outdated quite a bit API wise (data/prototype still is mostly fine i think), try this
http://lua-api.factorio.com/latest/index.html
and to get the correct surface i'd do something like:
Code: Select all
local surface = entity.surface
entity.destroy()
surface.create_entity(...)
Re: A few Questions
Posted: Wed Jul 27, 2016 11:02 am
by kasandraen
kasandraen wrote:Choumiko wrote:There is a typo in DecreasePriority: entity.destoy instead of dest
roy(), and you might do the destruction just before creating the new entity, so the player does not have to place it again in case the generator is missing
2: try using something like the following in your data.lua or data-updates, data-final-fixes where you create the prototypes
Code: Select all
for _, generator in pairs() do
generator.localized_name = {"generator_priority_terciary", "entity-name." .. generator.name}
end
Typos etc aside this should create something like "Steam engine (terciary)" if you have a local in your mod
Code: Select all
generator_priority_terciary = __1__ (terciary)
From
viewtopic.php?f=93&t=6847&start=290#p179290
Oh my god... I sat here pondering for hours what I had done wrong with the destroy thing... missing a a r......
And ill try the locale thing, thanks!

The mod is fully working now but,I.. dont fully understand the localization partm could you ELI5? :p
Code: Select all
function GeneratorCreatePrimary(base)
local obj = util.table.deepcopy(base)
local NAME = base.name
GeneratorCreateItem(data.raw["item"][NAME],"Primary")
obj.name = "GP-".. NAME.. "-Primary"
obj.energy_source.usage_priority = "primary-output"
obj.localized_name = {"generator_priority_primary", "entity-name." .. obj.name}
data.raw["generator"][obj.name] = obj
end
and the locale is:
Code: Select all
[controls]
increase-generator-priority= Increase generator output priority
decrease-generator-priority= Decrease generator output priority
[item-group-name]
GP-Hidden =
[entity-name]
generator_priority_primary = __1__ (primary)
generator_priority_secondary = __1__ (secondary)
generator_priority_terciary = __1__ (terciary)