1. Hey! Guest! The 35th GMC Jam will take place between November 28th, 12:00 UTC - December 2nd, 12:00 UTC. Why not join in! Click here to find out more!
    Dismiss Notice

Removing one value from array and deleting the cleared spot

Discussion in 'Programming' started by Bonemarrow, Oct 31, 2016.

  1. Bonemarrow

    Bonemarrow Guest

    Quick question.
    So I have an array called Items. And this array holds big amount of items. If I wanted to remove one value from it, and then cleaning the spot, how would I do it?
    Items: 1,2,3,4,5,6, to Items: 1,2,3,5,6,
    What I don't want: 1,2,3,0,5,6,

    Hope you understood.
    In advance-thank you.
  2. Online Handle

    Online Handle Member

    Oct 11, 2016
    It would be better to use a list to do this, then iterate through the list to find the item you want to remove, then remove it from the list.
    The list will automatically shrink or grow depending on if you are removing or adding items.
    ds_list documentation: http://docs.yoyogames.com/source/dadiospice/002_reference/data structures/ds lists/

    However, if you want to use a array of fixed size, what you will need to do is have a counter which keeps track of the last index in the array. This counter will also be used to determine the array size.
    So, let's say when we initialize our array we do:
    ItemSize = 6;
    Items[ItemSize+1] = 0; //initializing indices 0-6 as zero
    Then, using a script that would be called from whatever object initialized the array, we can add items by:
    var _obj = argument0;
    if(ItemSize >= array_length_1d(Items)){
      return false; //cannot add anything, array is full
    //else add to end of array which we know is open
    ItemSize += 1;
    Items[ItemSize] = _obj;
    return true;
    And for removing we would have to remove from the index, then shift everything over, and decrement the array. I will update this shortly with how to do that.

    Here is the remove script (assuming we know the index we want to remove from) :
    for(var i = argument0; i < ItemSize-1; i++){
      Items[i] = Items[i+1];
    ItemSize -= 1;
    If we need a script to find that index:
    for(var i = 0; i < ItemSize; i++){
      if(Items[i] == argument0){
        return i;
    Now, with those two scripts we can make a script to remove an object:
    Last edited: Oct 31, 2016
  3. ZeDuval

    ZeDuval Member

    Jun 20, 2016
    This is not really possible, you'd need to always create the array anew and fill it with the values minus the deleted ones. Your case is a perfect example for the "array or ds_list?" question, where the answer is ds_list.

    Will the data change, take a list is the data static, take an array.
  4. DariusWolfe

    DariusWolfe Member

    Sep 23, 2016
    There are a couple ways.

    First, don't use an array. ds_lists handle stuff like this for you, and are faster than arrays. They do require more complex commands to access and modify, but they're a superior option to arrays in most cases; If you delete a node in the list, it automatically fills the gap for you.

    Secondly, if you must use an array, you'll need to do the clean up yourself.

    Basically, if you delete position 3 in an array, you'll need to assign the value in position 4 to position 3, the value in position 5 to position 4, and so on; Don't forget to reduce whatever variable you're using to keep track of the length of the array.
  5. izanbf1803

    izanbf1803 Guest


    for (var i = 1; i < length(array); i++){
    if array[ i ] != elementToRemove {
    append(newArray, array[ i ]);
    Last edited by a moderator: Oct 31, 2016
  6. Bonemarrow

    Bonemarrow Guest

    Thank all of you for your replies.
    Online handle, I used your method but had to modify it a bit, howewer now it works! Thanks! Final code:

    for(var i = 0; i < inventoryItemAmount; i++){
    if(inventoryItemListTemp == argument0){
    return i;

    for(var i = 0; i < argument0; i++){
    inventoryItemListTempFinal = inventoryItemListTemp;
    for(var i = argument0+1; i < inventoryItemAmount; i++){
    inventoryItemListTempFinal[i-1] = inventoryItemListTemp;
    inventoryItemAmount -= 1;
    Online Handle and Yal like this.

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice