[Rseding91] [1.1.109] [API] Inconsistent shortcut toggle behavior when 'toggleable' is not set

This subforum contains all the issues which we already resolved.
Post Reply
Subject314159
Burner Inserter
Burner Inserter
Posts: 8
Joined: Thu Oct 12, 2023 8:53 pm
Contact:

[Rseding91] [1.1.109] [API] Inconsistent shortcut toggle behavior when 'toggleable' is not set

Post by Subject314159 »

When a shortcut.toggleable is not set the shortcut's visual toggle behavior gives inconsistent results.

Observed behavior:
Given below code, player.set_shortcut_toggled gives inconsistent results depending on when a shortcut button is pressed or when a custom input is given to call a generic toggle() function. When calling from the button press the button is toggled/untoggled visually, while calling from the custom input the button doesn't change visually. The button state as retrieved from player.is_shortcut_toggled is still updated in both cases. When setting shortcut.toggleable in data.lua to true the button visually behaves as expected.

Expected behavior when shortcut.toggleable is false:
- calling player.set_shortcut_toggled raises an error, or
- calling player.set_shortcut_toggled doesn't update the toggle state, both visually and as retrieved via player.is_shortcut_toggled

data.lua:

Code: Select all

data:extend({{
    type = "custom-input",
    name = "my_shortcut",
    key_sequence = "ALT + L",
    action = "lua"
}, {
    type = "shortcut",
    name = "my_shortcut",
    action = "lua",
    -- toggleable = true, -- When this setting is not set the behavior is unpredictable
    icon = {
        filename = "__base__/graphics/icons/fish.png",
        size = 64,
        mipmap_count = 4
    }
}})
control.lua:

Code: Select all

local toggle = function(player)
    player.set_shortcut_toggled("my_shortcut", not player.is_shortcut_toggled("my_shortcut"))
    log("toggled")
    if player.is_shortcut_toggled("my_shortcut") then
        log("Button is now on")
    else
        log("Button is now off")
    end
end

script.on_event(defines.events.on_lua_shortcut, function(e)
    local player = game.get_player(e.player_index)
    if not player then
        return
    end
    if e.prototype_name == "my_shortcut" then
        log("on_shortcut click @ tick " .. game.tick)
        toggle(player)
        -- Here the shortcut button is visually correctly toggled
    end
end)

script.on_event("my_shortcut", function(e)
    local player = game.get_player(e.player_index)
    log("on_event hotkey @ tick " .. game.tick)
    toggle(player)
    -- Here the button is visually not toggled
end)
log output:

Code: Select all

 -- Clicking the button to toggle/untoggle
 530.957 Script @__AAA-test__/control.lua:70: on_shortcut click @ tick 483
 530.957 Script @__AAA-test__/control.lua:56: toggled
 530.957 Script @__AAA-test__/control.lua:58: Button is now on
 -- Visually the button is now on (yellow)
 532.307 Script @__AAA-test__/control.lua:70: on_shortcut click @ tick 564
 532.307 Script @__AAA-test__/control.lua:56: toggled
 532.307 Script @__AAA-test__/control.lua:60: Button is now off
 -- Visually the button is now off (grey)
 -- Providing the custom input
 533.557 Script @__AAA-test__/control.lua:78: on_event hotkey @ tick 639
 533.557 Script @__AAA-test__/control.lua:56: toggled
 533.557 Script @__AAA-test__/control.lua:58: Button is now on
 -- Visually the button is still off (grey), while state is toggled
 534.590 Script @__AAA-test__/control.lua:78: on_event hotkey @ tick 670
 534.590 Script @__AAA-test__/control.lua:56: toggled
 534.590 Script @__AAA-test__/control.lua:60: Button is now off
 -- Visually the button is still off (grey) and toggled state matches again
 --Setting the button toggled by clicking the button
 536.307 Script @__AAA-test__/control.lua:78: on_event hotkey @ tick 773
 536.307 Script @__AAA-test__/control.lua:56: toggled
 536.307 Script @__AAA-test__/control.lua:58: Button is now on
 -- Button is indeed on (yellow)
 -- Untoggling by button press
 537.273 Script @__AAA-test__/control.lua:70: on_shortcut click @ tick 802
 537.273 Script @__AAA-test__/control.lua:56: toggled
 537.273 Script @__AAA-test__/control.lua:60: Button is now off
 -- The button is still off (grey) as expected
 -- Change the button to on (yellow) by clicking the button
 38.607 Script @__AAA-test__/control.lua:70: on_shortcut click @ tick 842
 538.607 Script @__AAA-test__/control.lua:56: toggled
 538.607 Script @__AAA-test__/control.lua:58: Button is now on
 -- The button is again on (yellow) as expected
 -- Turning the button off (grey) by custom input
 539.240 Script @__AAA-test__/control.lua:78: on_event hotkey @ tick 861
 539.240 Script @__AAA-test__/control.lua:56: toggled
 539.240 Script @__AAA-test__/control.lua:60: Button is now off
 --The button remains on (yellow)

Rseding91
Factorio Staff
Factorio Staff
Posts: 13929
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: [Rseding91] [1.1.109] [API] Inconsistent shortcut toggle behavior when 'toggleable' is not set

Post by Rseding91 »

Thanks for the report. I've changed it for the next release so if a shortcut is marked as not-toggleable it will error if attempting to set it as toggled through the Lua API.
If you want to get ahold of me I'm almost always on Discord.

Post Reply

Return to “Resolved Problems and Bugs”