I'm new into Factorio modding, but I have dabbled a bit in Lua. Too little to call myself "experienced", but I have encountered Lua's weirdness ("everything's a table", lol).
I'm the author of a mod called "BotPrioritizer", that I initially thought to be very simple. My mod gives personal robots priority, by cancelling all bot work orders and reissuing them again, so personal robots can take over. Easy, right? Deconstruct everything, Ctrl+Z... nope! But i digress.
So my problem is, that I want to cancel selected upgrades and re-issue them again. I'm checking if an entity is to_be_upgraded(), which returns true. But if I try to get the upgrade target using get_upgrade_target(), I don't get any result back and my variable stays nil.
Here's the code I'm using; it runs at on_player_selected_area and on_player_alt_selected_area and I'm using a selection-tool prototype to make the selection:
Code: Select all
local function handle_selection(event)
if not event.item == 'bot-prioritizer' then return end
-- Main logic
local player = game.get_player(event.player_index)
local force = player.force
for _, entity in ipairs(event.entities) do
if entity.valid then
if entity.type == "entity-ghost" or entity.type == "tile-ghost" then -- handle ghosts
if entity.clone({position = entity.position, force = entity.force}) then
entity.destroy()
end
elseif entity ~= nil and entity.to_be_deconstructed() then -- handle entities to be deconstructed
entity.cancel_deconstruction(force)
entity.order_deconstruction(force)
-- THIS SECTION DOESN'T WORK
elseif entity ~= nil and entity.to_be_upgraded() then -- handle upgrades
local upgrade_proto = entity.get_upgrade_target()
if upgrade_proto then
entity.cancel_upgrade(force, player)
entity.order_upgrade({force = entity.force, target = upgrade_proto, player = player})
else
player.print("ERROR: Couldn't find out upgrade target.")
end
end
-- SECTION END
end
end
-- Nexela's input for handling tiles
for _, tile in pairs(event.tiles) do
if tile.valid then
local pos = tile.position
pos.x, pos.y = pos.x + .5, pos.y + .5
if event.surface.find_entity('deconstructible-tile-proxy', pos) then
tile.cancel_deconstruction(force, player)
tile.order_deconstruction(force, player)
end
end
end
end
Currently I do have a workaround using the events on_marked_for_upgrade and on_cancelled_upgrade to keep track of all the upgrades myself in a global table. Of course there's also a cleanup routine to purge old entities out of this table.
It works, but I'd rather use the built-in API and I find it so weird, that this function doesn't work for me!
What's going on with this function?
Best regards,
Chris