Page 1 of 1

[Solved] How to make a GUI button to give a selection tool ?

Posted: Thu Jul 16, 2020 1:42 pm
by Creidhne
Hi,
I would like to make a GUI button that gives a custom "selection-tool" item when clicked. Exactly like a shortcut button, I just want to place it somewhere else with a specific caption.

This is my best attempt so far:

Code: Select all

-- player: https://lua-api.factorio.com/latest/LuaPlayer.html

player.clean_cursor()
player.cursor_stack.set_stack{
    name = "my-selection-tool",
}
It works. But when the player cleans his cursor, the item is placed in his inventory. I tried to add the following line to fix this, but it didn't change anything:

Code: Select all

player.hand_location = nil
Any suggestion to make it work ?
Thanks in advance.

Re: How to make a GUI button to give a selection tool ?

Posted: Thu Jul 16, 2020 3:52 pm
by DaveMcW

Code: Select all

script.on_event(defines.events.on_player_cursor_stack_changed, function(event)
  local player = game.players[event.player_index]
  player.get_main_inventory().remove("my-selection-tool")
end)

Re: How to make a GUI button to give a selection tool ?

Posted: Thu Jul 16, 2020 4:21 pm
by Creidhne
Hi,
thanks for the quick answer. It almost fixes the issue, except if the inventory is full. The player gets a "Cannot put away XXX. Player inventory is full" message, and this event isn't triggered (the cursor stack isn't modified).

Re: How to make a GUI button to give a selection tool ?

Posted: Thu Jul 16, 2020 4:27 pm
by Bilka
If you want it to behave the same way as copy paste etc, use the "only-in-cursor" prototype flag (https://wiki.factorio.com/Types/ItemPro ... -cursor.22).

Re: [Solved] How to make a GUI button to give a selection tool ?

Posted: Thu Jul 16, 2020 4:41 pm
by Creidhne
"only-in-cursor" works perfectly for my use case.

Thanks for the tip :-)