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
}
}})
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)
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)