How do I remove an item from a drop-down?

Place to get help with not working mods / modding interface.
User avatar
Gangsir
Inserter
Inserter
Posts: 20
Joined: Fri Jul 15, 2016 4:50 pm
Contact:

How do I remove an item from a drop-down?

Post by Gangsir »

I can't seem to figure out how to remove a specific index from a drop-down. Trying to manipulate items via

Code: Select all

table.remove(dropDown.items, dropDown.selected_index)
doesn't seem to work, and there's no method to remove at a specific index.

Do I have to clear and recreate in order to remove an item? I'd really like not to.
I'm an administrator over at the Factorio wiki. If you have any questions about the wiki, or concerns, please do not hesitate to contact me. I'm on the subreddit (/r/factorio, /u/Gangsir) as well.
kikker450
Inserter
Inserter
Posts: 30
Joined: Fri May 05, 2017 9:07 am
Contact:

Re: How do I remove an item from a drop-down?

Post by kikker450 »

check the lua commands next time before asking these kinds of questions.

you have at least two options:

Code: Select all

drop_down.items[drop_down.selected_index] = nil 
dorp_down.set_item(drop_down.selected_index, nil)
Rseding91
Factorio Staff
Factorio Staff
Posts: 14301
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How do I remove an item from a drop-down?

Post by Rseding91 »

kikker450 wrote:check the lua commands next time before asking these kinds of questions.

you have at least two options:

Code: Select all

drop_down.items[drop_down.selected_index] = nil 
dorp_down.set_item(drop_down.selected_index, nil)
That doesn't do what he asked to do.
If you want to get ahold of me I'm almost always on Discord.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by bobingabout »

The fact that table.remove MUST accept a NUMBER as the second value really has me pulling my hair out at times. Trying to get an table entry number for a table full of tags is... annoying. No wonder I'm going bald.

in fact, that's exactly what I ended up doing for my techsave mod, in the event that a force is deleted, I create a new table, and copy every entry to it one by one, minus the one that doesn't exist anymore, then save it over the old table. Thank god this mod is obsolete now.

An example of something I do more regularly:
Say I want to remove the result line of a recipe.... (Say, because you've changed to a results line during a script that adds extra items on the output) It's easier just to redefine the entire recipe than it is to delete the line!
I'd like to be able to do something like...

Code: Select all

table.remove(recipe,"result")
Sure, in this specific case I can just set result = nil, but that doesn't feel right.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
ibeatbabybiters
Inserter
Inserter
Posts: 30
Joined: Sun Apr 30, 2017 7:14 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by ibeatbabybiters »

@bobingabout: As a JS developer I work with immutable objects (which are the same as tables in Lua) regularly, and since mutating objects directly is not allowed I have to create copies of objects. Here is one simple utility function (adapted to Lua) I use to create a copy of an object minus some properties I don't want:

Code: Select all

function copy_table(table, omit_callback)
  local result = {}
  for i, v in pair(table) do
    if not omit_callback(i, v) then
      result[i] = v
    end
  end
  return result
end
For example, to omit certain properties you can use it like this (I use a hashmap because it's slightly more efficient than iterating an array for every property):

Code: Select all

local props_to_omit = {
  foo = true,
  bar = true,
  result = true
}
-- copy will contain every property from some_table except foo, bar and result
local copy = copy_table(some_table, function (key) return props_to_omit[key] end)
I wrote this off the top of my head so haven't tested it, but it should work fine. The clone_table function can be adjusted however you want, for example instead of a callback function you could pass a single string (to omit only 1 property) or an array of strings/hash map. I'm using callback functions because of the greater flexibility.
Usually I don't beat babies, but when I do I beat baby biters.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14301
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How do I remove an item from a drop-down?

Post by Rseding91 »

bobingabout wrote:Sure, in this specific case I can just set result = nil, but that doesn't feel right.
That's the official (correct) way to remove a key from a table in Lua :P

Code: Select all

variable["result"] = nil -- removes the key "result" from the table "variable".
If you want to get ahold of me I'm almost always on Discord.
ibeatbabybiters
Inserter
Inserter
Posts: 30
Joined: Sun Apr 30, 2017 7:14 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by ibeatbabybiters »

Rseding91 wrote:
bobingabout wrote:Sure, in this specific case I can just set result = nil, but that doesn't feel right.
That's the official (correct) way to remove a key from a table in Lua :P

Code: Select all

variable["result"] = nil -- removes the key "result" from the table "variable".
That's good to know :D I was missing the "delete" keyword in Lua that JS has. Now I also understand why I couldn't create gaps in an array by using nil, e.g. {nil, nil, nil} will produce an empty table {}, while in JS it is possible to have an array full of undefineds/nils.
Usually I don't beat babies, but when I do I beat baby biters.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by bobingabout »

setting a variable to nil can't be the proper way to delete it. I've read some LUA things before that sugest if you do that, then when you later do a for ipairs, it will drop out at where you set the nil.
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
Nexela
Smart Inserter
Smart Inserter
Posts: 1828
Joined: Wed May 25, 2016 11:09 am
Contact:

Re: How do I remove an item from a drop-down?

Post by Nexela »

variable["result"] result is a named key setting it to nil is the correct way as ipairs doesn't work on it anyway. If you need to keep an ordered array ordered then use table.remove(table, index)

for stuff in data.raw my guess is (and I could be wrong) that factorio c code will handle converting the skipped stuff in an ordered arrary correctly for use in the game.
Rseding91
Factorio Staff
Factorio Staff
Posts: 14301
Joined: Wed Jun 11, 2014 5:23 am
Contact:

Re: How do I remove an item from a drop-down?

Post by Rseding91 »

Nexela wrote:variable["result"] result is a named key setting it to nil is the correct way as ipairs doesn't work on it anyway. If you need to keep an ordered array ordered then use table.remove(table, index)

for stuff in data.raw my guess is (and I could be wrong) that factorio c code will handle converting the skipped stuff in an ordered arrary correctly for use in the game.
Correct. Also ipairs is virtually never needed - just use pairs and it works the same except you don't get these annoying problems. Also also: Factorio is written in C++ - not C :)

Also also also, why aren't you on IRC or Discord?
If you want to get ahold of me I'm almost always on Discord.
ibeatbabybiters
Inserter
Inserter
Posts: 30
Joined: Sun Apr 30, 2017 7:14 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by ibeatbabybiters »

I guess Gangsir learned everything here except how to remove an item drop a dropdown :D

I haven't tried it myself, but now I'm curious, how does one remove an item from a dropdown? Looking at the docs I would have guessed that what kikker450 said is correct, although it seems "items" is a plain array of string, so the dropdown won't re-render if it's mutated directly? So is it necessary to store the items array in a local variable, remove the desired item and assign it back to the dropdown?
Usually I don't beat babies, but when I do I beat baby biters.
User avatar
Gangsir
Inserter
Inserter
Posts: 20
Joined: Fri Jul 15, 2016 4:50 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by Gangsir »

I finally figured it out, you have to pull the table of items out of the dropdown, then set it back in, eg

Code: Select all

local items = dropDown.items
table.remove(items, 1)
dropDown.items = items
I'm not sure why I had to do that, probably due to an interfacing issue, but either way, that worked out for me, here.
I'm an administrator over at the Factorio wiki. If you have any questions about the wiki, or concerns, please do not hesitate to contact me. I'm on the subreddit (/r/factorio, /u/Gangsir) as well.
User avatar
bobingabout
Smart Inserter
Smart Inserter
Posts: 7352
Joined: Fri May 09, 2014 1:01 pm
Contact:

Re: How do I remove an item from a drop-down?

Post by bobingabout »

Rseding91 wrote:Also also also, why aren't you on IRC or Discord?
Because I don't use either of those. I use Skype and Telegram. I don't use Discord for a good reason, last time I tried it, it was a bug riddled piece of shit that locked up my computer for a good 20 minutes before it would let me do anything. Then I tried to "search" for a friend to be greeted with a "you need to add their 4 digit number to this name so that we know which one you are looking for". If I knew the number, I wouldn't be searching for it!
Creator of Bob's mods. Expanding your gameplay since version 0.9.8.
I also have a Patreon.
Post Reply

Return to “Modding help”