• Hey Guest! Ever feel like entering a Game Jam, but the time limit is always too much pressure? We get it... You lead a hectic life and dedicating 3 whole days to make a game just doesn't work for you! So, why not enter the GMC SLOW JAM? Take your time! Kick back and make your game over 4 months! Interested? Then just click here!

SOLVED Delete array value/item???

FoxyOfJungle

Kazan Games
There is no built-in function to delete an item from an array, just like we have in ds_list_delete(id, pos). (EDIT: This function was added on GMS 2.3.1)

How do I delete 1 item in an array, but preserve the rest? And I need to delete this item anywhere in the array, be it the first item, the middle item or the last...

Example:

GML:
array[0] = "value 0";
array[1] = "value 1";
array[2] = "value 2";
array[3] = "value 3";
array[4] = "value 4";
array[5] = "value 5";
I want to delete the array[3]
or using another variable, like array[n]



I have this function, but it doesn't always work, if I try to delete the last item it works, but if it is a middle item, it simply breaks the array and stops working completely...

GML:
function array_delete(array, pos)
{
    /// @func array_delete(array, pos)
    /// @arg {array} array The array to delete from
    /// @arg {real} pos The position to delete
    // returns a new array or -1 when the array is empty

    var a = array;
    var i = pos;
    var r = -1;
    var L = array_length(a);
    if (L > 1)
    {
        r = array_create(L-1);
        array_copy(r, 0, a, 0, i);
        array_copy(r, i, a, i+1, L-i+1);
    }
    return r;
}


I'm currently using this, but it only deletes the last item in the array.

GML:
array_resize(workplace_room, array_length(workplace_room)-1);
workplace_room_index = array_length(workplace_room)-1;



PS: I can't use ds_list on my system, so I really need to use arrays. I will be very grateful if you can help me!
Thanks.
 
Last edited:

FrostyCat

Redemption Seeker
Trace your code with a 6-entry array trying to remove index 3. L-i+1 is 6-3+1=4 and i+1 is 3+1=4, so your code would copy 4 more entries starting from index 4. See the problem?
GML:
array_copy(r, 0, a, 0, i);
array_copy(r, i, a, i+1, L-i-1);
 

samspade

Member
There is no built-in function to delete an item from an array, just like we have in ds_list_delete(id, pos).

How do I delete 1 item in an array, but preserve the rest? And I need to delete this item anywhere in the array, be it the first item, the middle item or the last...

Example:

GML:
array[0] = "value 0";
array[1] = "value 1";
array[2] = "value 2";
array[3] = "value 3";
array[4] = "value 4";
array[5] = "value 5";
I want to delete the array[3]
or using another variable, like array[n]



I have this function, but it doesn't always work, if I try to delete the last item it works, but if it is a middle item, it simply breaks the array and stops working completely...

GML:
function array_delete(array, pos)
{
    /// @func array_delete(array, pos)
    /// @arg {array} array The array to delete from
    /// @arg {real} pos The position to delete
    // returns a new array or -1 when the array is empty

    var a = array;
    var i = pos;
    var r = -1;
    var L = array_length(a);
    if (L > 1)
    {
        r = array_create(L-1);
        array_copy(r, 0, a, 0, i);
        array_copy(r, i, a, i+1, L-i+1);
    }
    return r;
}


I'm currently using this, but it only deletes the last item in the array.

GML:
array_resize(workplace_room, array_length(workplace_room)-1);
workplace_room_index = array_length(workplace_room)-1;



PS: I can't use ds_list on my system, so I really need to use arrays. I will be very grateful if you can help me!
Thanks.
I have an array delete function for 2.3 and prior here: https://marketplace.yoyogames.com/assets/8736/array-functions (code also on github). (Also, 2.3.1 has a built in array_delete function.)
 

FoxyOfJungle

Kazan Games
Trace your code with a 6-entry array trying to remove index 3. L-i+1 is 6-3+1=4 and i+1 is 3+1=4, so your code would copy 4 more entries starting from index 4. See the problem?
GML:
array_copy(r, 0, a, 0, i);
array_copy(r, i, a, i+1, L-i-1);
I do not understand, he continues with the same problem, see:


The GIF is long!, at the end, notice that it still doesn't work. šŸ™


(Also, 2.3.1 has a built in array_delete function.)
OH YEAH!
I will test your link, thank you! If it works, I'll come back here.
 

Bulletech

Member
For Pre-2.3.1 this should work:
GML:
//Not tested
function array_delete_method1(_array,_position) {
    var _length = array_length(_array);
    if (_length > 1) {
        var _newArray = array_create(_length-1);
        var _j = 0;
        for (var _i=0; _i<_length; _i++) {
            if (_i == _position) {
                continue;
            }
            _newArray[_j] = _array[_i];
            _j += 1;
        }
        return _newArray;
    }
}
//example call:
myArray = array_delete_method1(myArray,3);
OR
GML:
//Not tested
function array_delete_method2(_array,_position) {
    var _length = array_length(_array);
    if (_length > 1) {
        for (var _i=_position; _i<_length-1; _i++) {
            _array[_i] = _array[_i+1];
        }
    }
    array_resize(_array,_length-1);
}
//example call:
array_delete_method2(myArray,3);
tbh I'm not sure how good either of these methods are
 
Last edited:

FoxyOfJungle

Kazan Games
For people in the future, this function works:

GML:
function array_delete_ext(array, pos)
{
    /// @func array_delete_ext(array, pos)
    /// @arg array
    /// @arg pos

    var _array_lenght = array_length(array);
    if (_array_lenght > 1)
    {
        var _array_new = array_create(_array_lenght - 1);
        var j = 0;
        for (var i=0; i<_array_lenght; i++)
        {
            if (i == pos)
            {
                continue;
            }
            _array_new[j] = array[i];
            j += 1;
        }
        return _array_new;
    }
}
@BulleTech Studios It is an "updated" version of your first method, thanks.
Thank you all for your help!
 

chamaeleon

Member
Hopefully you don't need to call your function with an array of length 1, wanting to remove the element at position 0.
GML:
function my_array_delete(arr, pos) {
    var arr_len = array_length(arr);
    if (arr_len > 0 && pos >= 0 && pos < arr_len) {
        var new_arr = array_create(arr_len-1);
        array_copy(new_arr, 0, arr, 0, pos);
        array_copy(new_arr, pos, arr, pos+1, arr_len-(pos+1));
        return new_arr;
    } else {
        return arr;
    }
}
GML:
var a = ["a", "b", "c", "d", "e"];
for (var i = 0; i < array_length(a); i++) {
    var b = my_array_delete(a, i);
    show_debug_message(string(i) + " -> " + string(b));
}
Code:
0 -> [ "b","c","d","e" ]
1 -> [ "a","c","d","e" ]
2 -> [ "a","b","d","e" ]
3 -> [ "a","b","c","e" ]
4 -> [ "a","b","c","d" ]
 
Top