Page 1 of 1

[1.1.76] on_built_entity loses stack on clear_cursor with blueprint

Posted: Wed Jan 18, 2023 2:19 am
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.

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

Posted: Wed Jan 18, 2023 3:41 am
by Rseding91
Thanks for the report however this is working as intended.

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

Posted: Wed Jan 18, 2023 4:40 am
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.

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

Posted: Wed Jan 18, 2023 5:21 am
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!