Here's how I achieved this.
In main.lua, within the sol.main:start_savegame(game) function:
-- ♡ Copying is an act of love. Please copy and share.
-- Returns an item for item_ command strings
function get_item_for_command(command)
local slot = tonumber(string.match(command, "^item_([12])$"))
if slot then
local item = game:get_item_assigned(slot)
return item
else
return nil
end
end
function game:on_command_pressed(command)
-- Handle items with item:on_command_pressed()
local item = get_item_for_command(command)
if item and item.on_command_pressed ~= nil then
item:on_command_pressed(command)
return true
else
-- Fall back to item:on_using()
return false
end
end
function game:on_command_released(command)
-- Handle items with item:on_command_released()
local item = get_item_for_command(command)
if item and item.on_command_released ~= nil then
item:on_command_released(command)
return true
else
return false
end
end
Then within the item itself, simply implement item:on_command_pressed() and item:on_command_released().
-- ♡ Copying is an act of love. Please copy and share.
function item:on_command_pressed(command)
print(string.format("%s command pressed", item:get_name()))
end
function item:on_command_released(command)
print(string.format("%s command released", item:get_name()))
end
In this case, item:on_using() is never called. If you choose not to implement item:on_command_pressed() for a particular item, it will fall back to item:on_using().
I'm a bit afraid I've missed something which I'll discover later, but this works in normal circumstances.