Blueprint Shotgun


Adds a gun that shoots items to build ghosts, upgrade entities, and more! Also features a vacuum mode to mine entities, tiles, and ground items.

Utilities
5 months ago
1.1
1.11K

b ✅ add crash protection in runtime

6 months ago

While I was playing I got the following error:

LuaItemStack API call when LuaItemStack was invalid for read.
stack traceback:
    [C]: in function '__index'
    __blueprint-shotgun__/scripts/mine/tiles.lua:61: in function 'process'
    __blueprint-shotgun__/control.lua:171: in function <__blueprint-shotgun__/control.lua:91>

To be honest, I don’t care what happened there, but I understood one thing - you are not using an isolated environment. It’s not scary if a mod deletes something, but it’s unpleasant when a mod crashes an entire server, so you need to make a pcall wrapper for each event, here’s one example:

local function e_on_tick(event)
    cliffs.on_tick(event)
    render.on_tick(event)
    flying_items.on_tick(event)
    sound.on_tick(event)
end

script.on_event(e.on_tick, function(event)
    local success, reason = pcall(e_on_tick, event)
    if not success then
        local message = "error while handling event: "..reason.."; event data: "..serpent.block(event)
        log(message)
        game.print(message)
    end
end)

this is worth doing for every dangerous event (this wrapper could definitely be simplified somehow, but for my purposes it was enough)

6 months ago

Could you show me your mod list? I haven't been able to reproduce this in vanilla.

Also, pcall should only be used when you cannot avoid an error, or you intentionally want to gracefully handle the error and continue as normal.
In Factorio modding. 99.9% of the time an error happens, it's because of an oversight by the mod author in some way.
Correctly handling an error with pcall would mean knowing what causes it and knowing what to do to prevent the mod from breaking.
If the mod author knew this information, why would they not just prevent the error from occuring in the first place?
Not to mention, if you don't know what's causing an error, and you pcall it and ignore it, there's no telling what else that error may have just screwed up for the rest of the mod's time in the save.
It could even cause more errors to happen down the line, and if those are getting ignored by the pcall, suddenly the mod becomes a buggy, janky mess.
In addition, mods erroring makes people much more likely to report the bug, since they can just copy the error directly from in game and paste it in the mod discussion page, like you have done here. Printing to console/log means they would have to find the log, copy just the error, and then post it, which is a significantly higher bar of effort to cross.

I'm sorry that my mod erroring has inconvenienced you, I truly am, but it's the most reliable way to get bugs reported and fixed.
Nobody else has told me to use pcall in my four years of modding, and because of that, I don't get many bug reports for my 30+ mods.
After all, the ideal number of errors is zero, and I'm happy to work with people to come closer to achieving that.

6 months ago

I'm sorry if you found it offensive, I was emotional because I ruined the evening for myself and my friends.
IMHO, error-free code is impossible, and I don’t see anything wrong with covering the code with some kind of “try/catch”, even if it slows it down a little, but it will eliminate the problematic situation when the host is a primitive console without any automation and in general, A couple of messages to the console is better than interrupting the game and rolling back your progress.

regarding the error, as can be seen from the tracelog - it happened while mining tiles, there was no one on the server except me, I was able to repeat the error once, but as soon as I connected the debugger... I couldn’t repeat it :(, pyanadon modpack and me I was mining py-limestone near the water, apparently this happened because there was a fish :)

6 months ago

as soon as I said! so, the file tiles.lua line 61, in temp_inventory there is 1 item <Empty LuaItemStack> and it has the following properties:
valid: true
valid_for_read: false

6 months ago

Looking up the code, I saw that in the entities table there is a single element with valid == false.
I didn’t look at the calls above because obviously an error occurs after an unsuccessful search for an entity, in fact I still don’t understand how to repeat the error again correctly

6 months ago
(updated 6 months ago)

I'm sorry if you found it offensive, I was emotional because I ruined the evening for myself and my friends.

No worries, a mod erroring is not desirable for anyone, we just see the solution to the problem in a different way.

This is a really weird bug, I have no idea what's causing it in this case, but I still thought of another way it can happen.
All it needed was a simple item.valid_for_read check.
Thanks for the report, fixed in 0.0.11

6 months ago

a good idea would be to look through the code to see where else the mod api gives valid and valid_for_read fields and check that they are both true

New response