GMS 2 delete elements of a list without messing up the list structure?

hello I have recently converted my ds_map of maps to a ds_list of maps for my ui elements.
I did this because the key was not needed but in fact I needed a index for updating my menu with key based input.

I however has come to another problem where the ui element do not keep their index when being deleted
I was wondering how to get around this?

this is my current refresh script
/// @description ui_refresh()

ui_advance = 0;
var _size = ds_list_size(ui_elements);
for(var i=0; i<_size; i++)
    var _element = ui_elements[| i];
    var _state = _element[? "STATE"];
    if (_state != ui_state) ds_list_delete(ui_elements, i);
and this is my current tab script

/// @description ui_tab()
/// @param string
/// @param sprite
/// @param index
/// @param up
/// @param down
/// @param left
/// @param right
/// @param rect

var _string = argument[0];
var _sprite = argument[1];
var _frame  =  0;
var _timer  = -1;
var _index  = argument[2];
var _state  = ui_state;
var _up     = argument[3];
var _down   = argument[4];
var _left   = argument[5];
var _right  = argument[6];
var _rect   = argument[7];

var _selected;
var _return;

if (ds_list_find_value(ui_elements, _index) == undefined)
    _element = ds_map_create();
    _element[? "STRING"] = _string;
    _element[? "SPRITE"] = _sprite;
    _element[? "FRAME"]  = _frame;
    _element[? "TIMER"]  = _timer;
    _element[? "INDEX"]  = _index;
    _element[? "STATE"]  = _state;
    _element[? "UP"]     = _up;  
    _element[? "DOWN"]   = _down; 
    _element[? "LEFT"]   = _left; 
    _element[? "RIGHT"]  = _right; 
    _element[? "RECT"]   = _rect; 
    ds_list_insert(ui_elements, _element, _index);
    if (ui_advance == 1) return 0;
    var _element = ds_list_find_value(ui_elements, _index);

    var _string = _element[? "STRING"];
    var _sprite = _element[? "SPRITE"];
    var _frame  = _element[? "FRAME"];
    var _timer  = _element[? "TIMER"];
    var _index  = _element[? "INDEX"];
    var _state  = _element[? "STATE"];
    var _up     = _element[? "UP"];
    var _down   = _element[? "DOWN"];
    var _left   = _element[? "LEFT"];
    var _right  = _element[? "RIGHT"];
    _frame    = 0;
    _return   = 0;
    _selected = 0;
    if      (_timer >  0) { _timer--; _frame = 0;}
    else if (_timer == 0) { _timer--; _frame = 2;}
    if (_index == ui_index) { _frame = 1; _selected = 1; _return = 1;}
    if (_selected)
        //if(keyboard_check_pressed(vk_enter)) { _timer = 3; _return = 1; }
        if(keyboard_check_pressed(vk_up))    ui_index = _up;
        if(keyboard_check_pressed(vk_down))  ui_index = _down;
        if(keyboard_check_pressed(vk_left))  ui_index = _left;
        if(keyboard_check_pressed(vk_right)) ui_index = _right;
        if (_index != ui_index)    { ui_advance = 1; }
    _element[? "STRING"] = _string;
    _element[? "SPRITE"] = _sprite;
    _element[? "FRAME"]  = _frame;
    _element[? "TIMER"]  = _timer;
    _element[? "INDEX"]  = _index;
    _element[? "STATE"]  = _state;
    _element[? "UP"]     = _up;  
    _element[? "DOWN"]   = _down; 
    _element[? "LEFT"]   = _left; 
    _element[? "RIGHT"]  = _right; 
    return _return;
I also have index based drawing
// @description ui_draw()

for(var i=0; i<ds_list_size(ui_elements); i++)
    var _element = ui_elements[| i];
    var _string  = _element[? "STRING"];
    var _sprite  = _element[? "SPRITE"];
    var _frame   = _element[? "FRAME"];
    var _rect       = _element[? "RECT"];
    draw_sprite_rectangle(_sprite[0], _frame, _rect[0], _rect[1], _rect[0]+_rect[2], _rect[1]+_rect[3], _sprite[5], _sprite[3], _sprite[6], _sprite[4]);
    draw_text(_rect[0]+_rect[2]/2, 2+_rect[1]+_rect[3]/2, _string);
    draw_text(_rect[0]+_rect[2]/2, 3+_rect[1]+_rect[3]/2, _string);
I was wondering what's the best way to update and delete this kind of element?


ᴏɴʟɪɴᴇ ᴍᴜʟᴛɪᴘʟᴀʏᴇʀ
Forum Staff
Should you want to remove an element while keeping it's index, you could change it to `undefined` or other "non-element" value.
It is, however, a sign of bigger troubles - why would you rely on indexes of elements in a list...
I wanted to have it index based so that I can have input based menus (where you press up, down, left, right, a and b to navigate the menu)
are you suggesting I use maps? because I tried that and keys are unnecessary for what I'm trying to do
ok so I got it working by just filling the list with undefined value and setting them when I need to
however I think this is a bug
this code
var _element = ds_list_find_value(ui_elements, _index)
is returning elements 2 and 3 even though my "_index" variable is 0 and 1