[1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Bugs that are actually features.
BinarySpike
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Jun 28, 2016 2:15 am
Contact:

[1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Post by BinarySpike »

Issue
Using a blueprint, if you use a `player.clear_cursor()` then subsequent enumerations of the `event.stack` provided by `defines.events.on_built_entity` fail.

Example:

Code: Select all

local counter = 0

script.on_event(defines.events.on_built_entity, function(event)
    game.print(tostring(counter) ..
        ": " .. tostring(event.stack.is_blueprint) .. " " .. tostring(event.stack.is_blueprint_book) .. " ")
    counter = counter + 1

    -->>>if not (event.stack.is_blueprint or event.stack.is_blueprint_book) then return end -- resolves to false after 1st on_built_entity

    local player = game.players[event.player_index]

    player.clear_cursor()

    local name = event.stack.name
    -- enumerating event.stack.name fails
    --[[
        Error while running event variable-blueprint::on_built_entity (ID 6)
        LuaItemStack API call when LuaItemStack was invalid for read.
    ]]
end)
Expectation
`clear_cursor()` should not clear `event.stack`. Additionally, I wouldn't expect subsequent `on_built_entity` to have valid `event.stack` as well.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14250
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Post by Rseding91 »

Thanks for the report however this is working as intended.
If you want to get ahold of me I'm almost always on Discord.
FuryoftheStars
Smart Inserter
Smart Inserter
Posts: 2768
Joined: Tue Apr 25, 2017 2:01 pm
Contact:

Re: [1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Post by FuryoftheStars »

BinarySpike wrote: Wed Jan 18, 2023 2:19 am Using a blueprint, if you use a `player.clear_cursor()` then subsequent enumerations of the `event.stack` provided by `defines.events.on_built_entity` fail.
If I'm understanding their usage correctly, event is a reference to data, not a copy of it. As such, calling clear_cursor() will clear the data at the source, and thus event.stack is now referring to something that has been cleared. As such, you should either wait to clear the cursor until after you've done everything you need to, or store the needed data away first.

Edit: Sorry, should've said that event is a table of references, not that it itself was a reference.
Last edited by FuryoftheStars on Wed Jan 18, 2023 5:40 am, edited 1 time in total.
My Mods: Classic Factorio Basic Oil Processing | Sulfur Production from Oils | Wood to Oil Processing | Infinite Resources - Normal Yield | Tree Saplings (Redux) | Alien Biomes Tweaked | Restrictions on Artificial Tiles | New Gear Girl & HR Graphics
BinarySpike
Burner Inserter
Burner Inserter
Posts: 16
Joined: Tue Jun 28, 2016 2:15 am
Contact:

Re: [1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Post by BinarySpike »

Rseding91 wrote: Wed Jan 18, 2023 3:41 am Thanks for the report however this is working as intended.
Can we get this added to the docs? Maybe, "A reference to the stack this comes from, for example the player's cursor"

Image

Thanks!
Post Reply

Return to “Not a bug”