[GUI] removing containers/items

Discussion of Lua and LuaWML support, development, and ideas.

Moderator: Forum Moderators

Post Reply
white_haired_uncle
Posts: 1207
Joined: August 26th, 2018, 11:46 pm
Location: A country place, far outside the Wire

[GUI] removing containers/items

Post by white_haired_uncle »

I have a multi_page object, where one page is a unit description (same page for any selected unit, not one page per unit). I've found that when I change units I need to clear out the old data (if unit A has 5 abilities, and unit B has 4 abilities, when I switch from A to B I see B's abilities overwrite the first 4, but the fifth remains). I figured the easiest method would just be to delete the page and rebuild it. So I tried:

Code: Select all

-- this is how they are created
dialog.unit_info_mp:add_item_of_type("empty_page")
dialog.unit_info_mp:add_item_of_type("unit_page")
-- here I want to reset the unit page
dialog.unit_info_mp:remove_items_at(2,1)
dialog.unit_info_mp:add_item_of_type("unit_page",2)  -- aborts

wesnoth: src/gui/widgets/generator_private.hpp:736: gui2::grid& gui2::generator<minimum_selection, maximum_selection, my_placement, select_action>::create_item(int, const gui2::builder_grid&, const widget_data&, const std::function<void(gui2::widget&)>&) [with minimum_selection = gui2::policy::minimum_selection::one_item; maximum_selection = gui2::policy::maximum_selection::one_item; my_placement = gui2::policy::placement::independent; select_action = gui2::policy::select_action::show; gui2::widget_data = std::map<std::__cxx11::basic_string<char>, std::map<std::__cxx11::basic_string<char>, t_string> >]: Assertion `index == -1 || static_cast<unsigned>(index) <= items_.size()' failed.
Abort(coredump)
spartan$ /opt/wesnoth-1.17-git/bin/wesnoth --debug --log-debug=scripting/lua
Battle for Wesnoth v1.17.25+dev (88befc29de9-Clean) x86_64
Ouch.

Okay, I just assumed the "position" was 2, since it was the second item I created, and that's how I've been referencing it. So I ran add_item_of_type with no position and captured the return. As the wiki says returned position is always positive, I found the output odd, so I tried creating a page twice and I got -- the same position?.

Code: Select all

local item,pos = dialog.unit_info_mp:add_item_of_type("unit_page")
chat(sf("%s %s,  %s %s",type(item),tostring(item),type(pos),tostring(pos)))
item,pos = dialog.unit_info_mp:add_item_of_type("unit_page")
chat(sf("%s %s,  %s %s",type(item),tostring(item),type(pos),tostring(pos)))

"userdata widget: 0x55b4ef445b30,  number -1"
"userdata widget: 0x55b4ef5ac370,  number -1"
At the moment, I'm simply attempting to delete the second item and creating a new one, and this seems to work:

Code: Select all

dialog.unit_info_mp:remove_items_at(2,1)
dialog.unit_info_mp:add_item_of_type("unit_page")
But I so don't like that. I don't know why the delete works at all. And I suspect there's some luck involved here as I'm deleting the last item and creating a new one so it makes sense they'd be in the same position, but what if I wanted to delete a random item? Let's say I delete item 1 (assuming I can) and create a new one, does old item 2 become item 1 and the new one is 2, or does the old one remain 2 and the new one 1 (the first empty slot in the list), or does the old one remain 2 and the new one 3 (appended to the list)?

It seems to me that the right way to do things would be to capture the position when I initially create the item, and then if I need to replace it use that as the position in remove_items_at and add_item_of_type. That way I don't have to care what "position" is, just store it.

Now clearly I'm confused here, but it sure looks to me like there's also something else wrong?

Also, does my general approach even make sense?

TIA
Speak softly, and carry Doombringer.
User avatar
Celtic_Minstrel
Developer
Posts: 2235
Joined: August 3rd, 2012, 11:26 pm
Location: Canada
Contact:

Re: [GUI] removing containers/items

Post by Celtic_Minstrel »

Well, I'm not entirely sure what you're doing wrong (or even if you're doing something wrong), but it definitely shouldn't crash. That's a bug for sure.
Author of The Black Cross of Aleron campaign and Default++ era.
Former maintainer of Steelhive.
Post Reply