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

Place to get help with not working mods / modding interface.
Post Reply
Creidhne
Inserter
Inserter
Posts: 28
Joined: Mon Jun 10, 2019 9:43 am
Contact:

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

Post 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.
Last edited by Creidhne on Thu Jul 16, 2020 4:38 pm, edited 1 time in total.

User avatar
DaveMcW
Smart Inserter
Smart Inserter
Posts: 3700
Joined: Tue May 13, 2014 11:06 am
Contact:

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

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

Creidhne
Inserter
Inserter
Posts: 28
Joined: Mon Jun 10, 2019 9:43 am
Contact:

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

Post 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).

Bilka
Factorio Staff
Factorio Staff
Posts: 3132
Joined: Sat Aug 13, 2016 9:20 am
Contact:

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

Post 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).
I'm an admin over at https://wiki.factorio.com. Feel free to contact me if there's anything wrong (or right) with it.

Creidhne
Inserter
Inserter
Posts: 28
Joined: Mon Jun 10, 2019 9:43 am
Contact:

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

Post by Creidhne »

"only-in-cursor" works perfectly for my use case.

Thanks for the tip :-)

Post Reply

Return to “Modding help”