Page 1 of 1
How do I remove an item from a drop-down?
Posted: Fri Jun 09, 2017 5:24 pm
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.
Re: How do I remove an item from a drop-down?
Posted: Sat Jun 10, 2017 9:31 pm
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)
Re: How do I remove an item from a drop-down?
Posted: Sat Jun 10, 2017 10:16 pm
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.
Re: How do I remove an item from a drop-down?
Posted: Mon Jun 12, 2017 9:12 am
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...
Sure, in this specific case I can just set result = nil, but that doesn't feel right.
Re: How do I remove an item from a drop-down?
Posted: Mon Jun 12, 2017 4:35 pm
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.
Re: How do I remove an item from a drop-down?
Posted: Mon Jun 12, 2017 6:43 pm
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
Code: Select all
variable["result"] = nil -- removes the key "result" from the table "variable".
Re: How do I remove an item from a drop-down?
Posted: Mon Jun 12, 2017 8:00 pm
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
Code: Select all
variable["result"] = nil -- removes the key "result" from the table "variable".
That's good to know
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.
Re: How do I remove an item from a drop-down?
Posted: Tue Jun 13, 2017 8:11 am
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.
Re: How do I remove an item from a drop-down?
Posted: Tue Jun 13, 2017 8:42 am
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.
Re: How do I remove an item from a drop-down?
Posted: Tue Jun 13, 2017 5:16 pm
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?
Re: How do I remove an item from a drop-down?
Posted: Tue Jun 13, 2017 7:21 pm
by ibeatbabybiters
I guess Gangsir learned everything here except how to remove an item drop a dropdown
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?
Re: How do I remove an item from a drop-down?
Posted: Wed Jun 14, 2017 5:49 pm
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.
Re: How do I remove an item from a drop-down?
Posted: Thu Jun 15, 2017 8:06 am
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!