FoxyOfJungle
Kazan Games
There is no built-in function to delete an item from an array, just like we have in
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:
I want to delete the
or using another variable, like
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...
I'm currently using this, but it only deletes the last item in the array.
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.
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";
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: